代理的使用

代理的使用

《1》导包
from urllib import request
,《2》定义要爬取的网站
url=’http://www.baidu.com/s?wd=ip
《3》分配数据
proxy={
‘http’: ‘http://xxxx
}
《4》创建handler用于实现代理请求到的数据的添加
handler=request.ProxyHandler(proxy)
《5》创建handler用于实现代理请求到的数据的添加
opener=request.build_opener(handler)
response=opener.open(url)
把获取的对象解读为html
html_bytes=response.read()
写入文件中
with open(‘daili.html’,’wb’) as ff:
ff.write(response.read())
注意坑:
《1》:https 有时间会爬取不到想要的数据
《2》:代理url的使用—-不好用的代理会使爬取得不到想要的数据

猜你喜欢

转载自blog.csdn.net/chengjintao1121/article/details/81812127