# -*- coding: utf-8 -*-
import urllib2
import cookielib
url="http://www.baidu.com" #define URL
# three ways to capturing webs
print "first way:"
response1 = urllib2.urlopen(url)
print response1.getcode()#return 200,the web can be accessible
print len(response1.read())# return the length of this web
print "second way:"
request = urllib2.Request(url)#调用request对象
request.add_header("user-agent" , "Mozilla/5.0")#把爬虫伪装成一个浏览器,Mozilla/5.0是火狐客户端浏览器版本
response2 = urllib2.urlopen(request)
print response2.getcode()
print len(response2.read())
print "third way:"
cj= cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print cj #打印cookie内容
print response3.read()
python_urllib2下载网页的三种方式
最新推荐文章于 2021-12-09 19:18:29 发布
本文介绍了使用Python的urllib2库抓取网页的方法,包括直接请求、设置头部信息及利用CookieJar处理Cookies等三种方式,并展示了如何获取HTTP状态码及页面长度。
3568

被折叠的 条评论
为什么被折叠?



