赞
踩
方法一、最简洁
- #coding:utf-8
- import urllib2
- import cookielib
-
- url='http://baidu.com'
-
- print "method 1"
- responce1=urllib2.urlopen(url)
- print responce1.getcode() #200
- print len(responce1.read()) #81
方法二、添加data、http header信息
- print "method 2"
- request=urllib2.Request(url)
- request.add_header("User-Agent","Mozilla/5.0")
- responce2=urllib2.urlopen(request)
- print responce2.getcode() #200
- print len(responce2.read()) #81
方法三、添加处理器
- print "method 3"
- cj=cookielib.CookieJar()
- opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
- urllib2.install_opener(opener)
- responce3=urllib2.urlopen(url)
-
- print cj #<CookieJar[]>
- print responce3.getcode() #200
- print responce3.read() #81
每个方法依次升级,应用于模拟更丰富的http请求下载网页。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。