python3爬虫之urllib初探
urllib主要包含request(请求模块)、error(异常处理模块)、parse(工具模块)、robotparser(识别网站的robots.txt文件,是否允许爬取)。
request(请求模块)
1、request.urlopen(发送请求)
import urllib.request response = urllib.request.urlopen('https://www.python.org') print(response.read().decode('utf-8'))
用法
urlopen所有参数
urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
1、url
必选:链接
2、data
可选,使用data是通过post传值,并且数据格式必须为bytes类型,使用方法如下:
import urllib.parse
import urllib.request data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf8')
response = urllib.request.urlopen('http://httpbin.org/post', data=data)
print(response.read())
3、timeout
参数用于设置超时时间,单位为秒,
import socket
import urllib.request
import urllib.error try:
response = urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)
except urllib.error.URLError as e:
if isinstance(e.reason, socket.timeout):
print('TIME OUT')
4、其他参数
context
参数,它必须是ssl.SSLContext
类型,用来指定SSL设置。此外,cafile
和capath
这两个参数分别指定CA证书和它的路径,这个在请求HTTPS链接时会有用。
cadefault
参数现在已经弃用了,其默认值为False
。
2、request.Request类
作用:构建完整的请求信息。
import urllib.request request = urllib.request.Request('https://python.org')
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
Request参数
urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)
1、url
2、data
data
如果要传,必须传bytes
(字节流)类型的。如果它是字典,可以先用urllib.parse
模块里的urlencode()
编码。
data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf8')
3、headers
headers
是一个字典,它就是请求头,我们可以在构造请求时通过headers
参数直接构造,也可以通过调用请求实例的add_header()
方法添加.
4、origin_req_host
请求方的host名称或者IP地址。
5、unverifiable
表示这个请求是否是无法验证的,默认是False
,意思就是说用户没有足够权限来选择接收这个请求的结果。例如,我们请求一个HTML文档中的图片,但是我们没有自动抓取图像的权限,这时unverifiable的值就是
True`。
6、method
是一个字符串,用来指示请求使用的方法,比如GET、POST和PUT等。
from urllib import request, parse url = 'http://httpbin.org/post'
headers = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
'Host': 'httpbin.org'
}
dict = {
'name': 'Germey'
}
data = bytes(parse.urlencode(dict), encoding='utf8')
req = request.Request(url=url, data=data, headers=headers, method='POST')
response = request.urlopen(req)
print(response.read().decode('utf-8'))
3、urllib高阶
https://cuiqingcai.com/5500.html停一下
最新文章
- 使用TARGET_IPHONE_SIMULATOR和TARGET_OS_IPHONE宏区分模拟器和设备
- 一、Spring——IoC
- CentOS6下搭建OpenVPN服务器
- POJ3267——The Cow Lexicon(动态规划)
- Spring Boot 学习(1)
- tuxedo开发
- 从线程池到synchronized关键字详解
- uva10256(计算几何)
- H5 38-背景图片和插入图片区别
- mysql optimize table
- Understanding Built-In User and Group Accounts in IIS 7
- 使用Oracle数据库时的org.springframework.jdbc.BadSqlGrammarException:
- .net MongoDB使用
- React多页面应用脚手架-v1.3.0
- shell 余弦值转角度
- 微信小程序跨页面获取数据示例
- BZOJ4446:[SCOI2015]小凸玩密室(树形DP)
- css 小问题解决方法整理
- JavaScript Debug 之 Console
- 【Python】关于Python里面小数点精度控制的问题
热门文章
- 统计学习方法——第四章朴素贝叶斯及c++实现
- C++中vecotr表示二维数组并自己实现一个Grid类
- python3-继承和多态
- 前端之JQuery:JQuery文档操作
- python-第三方包的安装和升级和卸载
- 超好用json转excel工具
- django快速搭建blog
- 【rust】Rust 的构建系统和包管理工具Cargo认识并初步使用(2)
- HDU 2825 Wireless Password ( Trie图 &;&; 状态压缩DP )
- [USACO10HOL]牛的政治Cow Politics