赞
踩
urllib是Python内置的库,requests是对urllib的再次封装。在使用上更加便捷。
不加headers信息可能会造成服务器拒绝下载网络图片到本地。或者是只能下载https
协议的url图片,不能下载http
协议的url图片。
import urllib
#from urllib import request
def add_header_to_retrieve():
headers=('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
add_header_to_retrieve()
urllib.urlretrieve(img_path, save_path)
import urllib
import requests
def add_header_to_retrieve():
headers=('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
add_header_to_retrieve()
urlretrieve(img_path, save_path)
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 ",
}
req = requests.get(headers=headers, url=img_path)
with open(save_path, 'wb') as f:
f.write(req.content)
数据推送时,需要加载headers头,否则会出现连接超时的错误。
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 ",
}
response = requests.post(url, files=files, data=datas, headers=headers)
参考资料
浅谈Python两大爬虫库——urllib库和requests库区别
Python使用urllib库、requests库下载图片的方法比较
如何给urllib.request.urlretrieve 加上header
【Python】Http Post请求四种请求体的Python实现
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。