一木.溪桥 在Logic Education跟Jerry学爬虫
07期:Python 爬虫 一木.溪桥学爬虫-03:请求模块urllib、 urllib.request、urllib.parse.urlencode、urllib.parse.quote(str)、parse.unquote()
日期:2021年1月26日
学习目标:
-
请求模块urllib
-
urllib.request
-
urllib.parse.urlencode
-
urllib.parse.quote(str)
-
parse.unquote()
-
urllib post 案例
学习内容:
爬虫请求模块
urllib
为什么学习 urllib?
- 有的一些比较老的爬虫项目用的是urllib
- 有时我们在做一些爬虫的时候往往需要requests + urllib 一起使用
- 是python内置的模块
- urllib在某些方面还是非常强大
urllib的快速入门
-
eg. 下载网上的一张图片
# 方法1--open, close import requests url = 'https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1603365312,' \ '3218205429&fm=26&gp=0.jpg' req = requests.get(url) fn = open('code.png', 'wb') # 文件命名为code.png,wb 写入二进制数据 fn.write(req.content) # content中间存的是字节码(此处图片存储的就是二进制数据),而text中存的是Beautifulsoup根据猜测的编码方式将content内容编码成字符串。 fn.close()
# 方法2--with open, 可以不用close() import requests url = 'https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1603365312,' \ '3218205429&fm=26&gp=0.jpg' req = requests.get(url) with open('code2.png', 'wb') as file_obj: file_obj.write(req.content)
# 方法3-- 用python内置模块 urllib 中的 request 方法 from urllib import request url = 'https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1603365312,' \ '3218205429&fm=26&gp=0.jpg' request.urlretrieve(url, 'code3.jpg') # url网址,文件名code3.jpg
urllib.request 模块
版本
-
python2 :urllib2、urllib
-
python3 :把urllib和urllib2合并
常用的方法:
- urllib.request.urlopen(“网址”) 作用 :向网站发起一个请求并获取响应
- 字节流 = response.read()
- 字符串 = response.read().decode(“utf-8”)
- urllib.request.Request"网址",headers=“字典”) urlopen()不支持重构User-Agent
响应对象
- read() 读取服务器响应的内容
- getcode() 返回HTTP的响应码
- geturl() 返回实际数据的URL(防止重定向问题)
import urllib.request
url