码迷,mamicode.com
首页 > 编程语言 > 详细

【Python】python3中urllib爬虫开发

时间:2017-12-07 22:43:33      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:font   uil   创建   data   head   roc   res   code   hand   

以下是三种方法

①First Method

最简单的方法

②添加data,http header

使用Request对象

③CookieJar

import urllib.request
from http import cookiejar
url =http://www.baidu.com

print("First Method")

response1 = urllib.request.urlopen(url)
#返回状态码
print(response1.getcode())
print(len(response1.read()))

print("Second Method")
request = urllib.request.Request(url)
request.add_header("uese-agent","Mazilla/5.0")
response2 = urllib.request.urlopen(url)
#返回状态码
print(response2.getcode())
print(len(response2.read()))

print("Third Method")
#声明一个CookieJar对象实例来保存cookie
cj = cookiejar.CookieJar()
#利用urllib.request库的HTTPCookieProcessor对象来创建cookie处理器,也就CookieHandler
handler = urllib.request.HTTPCookieProcessor(cj)
#通过CookieHandler创建opener
opener = urllib.request.build_opener(handler)
#此处的open方法同urllib.request的urlopen方法,也可以传入request
response3 = opener.open(url)
#返回状态码
print(response3.getcode())
print(response3.read())

【Python】python3中urllib爬虫开发

标签:font   uil   创建   data   head   roc   res   code   hand   

原文地址:http://www.cnblogs.com/OliverQin/p/8001259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!