python3使用代理ip伪装爬虫访问网站
来源:互联网 发布:c语言截断 编辑:程序博客网 时间:2024/06/05 16:12
这是学习小甲鱼的视频写的一毛一样的程序
'使用代理ip伪装爬虫''''使用代理第一步: proxy_support = urllib.request.ProxyHandler({})使用代理第二步: opener = urllib.request.build_opener(proxy_support)使用代理第三步: urllib.request.install_opener(opener);opener.open(url)'''import urllib.requesturl = 'http://www.whatismyip.com.tw'proxy_handle = urllib.request.ProxyHandler({'http':'220.249.185.178:9999'})#{'类型':'代理ip:端口'}opener = urllib.request.build_opener(proxy_handle)#创建一个openerurllib.request.install_opener(opener)#安装一个openerresponse = urllib.request.urlopen(url)html = response.read().decode('utf-8')#访问opener,返还给response,将response解码print(html)
报错:
urllib.error.URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>
找腿子,分析一波,是www.whatismyip.com.tw这个网站被爬的太多次,就和之前的有道一般,搜拉拉的升级了网站,不能用python直接刚,然后腿子加了个headers,换个ip,成功执行,与想象中的效果一样。下面是润色过的代码:
'代理ip'import urllib.requestimport urllib.errorurl = 'http://www.whatismyip.com.tw'proxy_handle = urllib.request.ProxyHandler({'http':'36.36.178.226:9797'})opener = urllib.request.build_opener(proxy_handle)urllib.request.install_opener(opener)headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}try: req = urllib.request.Request(url,headers=headers) response = urllib.request.urlopen(req) html = response.read().decode('utf-8') print(html)except urllib.error.URLError as e: print(e.reason)
阅读全文
0 0
- python3使用代理ip伪装爬虫访问网站
- python3爬虫伪装代理IP
- python3使用代理ip访问指定网站
- python3使用多代理访问网站
- python3使用多代理访问网站
- python3爬虫之IP代理
- Python3 爬虫--伪装
- Python3 爬虫使用User Agent和代理IP隐藏身份
- python3 使用代理访问
- python3使用ip代理池
- 爬虫伪装成浏览器访问
- Jsoup使用代理ip爬虫
- 如何使用ip代理爬虫
- python3实现网络爬虫(7)-- 使用ip代理抓取网页
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份
- Python3网络爬虫_使用User Agent和代理IP隐藏身份
- Python3爬虫之代理的使用
- 加密破解分析2
- 设计模式初探之一------工厂模式
- MyBatis原理示例
- 欢迎使用CSDN-markdown编辑器
- 修改vsftpd的默认根目录
- python3使用代理ip伪装爬虫访问网站
- TextView 、EditText 、Shape外形资源 、Selector选择器
- 老司机带你认识服务器硬盘--SAS和SATA盘
- js实现移动端漂亮input框上传本地,图片显示缩略图,点击可以查看大图,再点击恢复
- Android Studio卡在refreshing gradle project的原因和快速解决办法
- python爬虫-lxml的使用
- Android-环境搭建
- 垃圾回收
- 5-JavaScript设计模式——链式编程