博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python爬虫学习笔记——防豆瓣反爬虫
阅读量:7169 次
发布时间:2019-06-29

本文共 1695 字,大约阅读时间需要 5 分钟。

开始慢慢测试爬虫以后会发现IP老被封,原因应该就是单位时间里面访问次数过多,虽然最简单的方法就是降低访问频率,但是又不想降低访问频率怎么办呢?查了一下最简单的方法就是使用转轮代理IP,网上找了一些方法和免费的代理IP,尝试了一下,可以成功,其中IP代理我使用的是http://www.xicidaili.com/nn/

获取Proxies的代码如下:

1 for page in range(1,5): 2     IPurl = 'http://www.xicidaili.com/nn/%s' %page 3     rIP=requests.get(IPurl,headers=headers) 4     IPContent=rIP.text 5     soupIP = BeautifulSoup(IPContent,"html5lib") 6     trs = soupIP.find_all('tr') 7     for tr in trs[1:]: 8         tds = tr.find_all('td') 9         ip = tds[2].text.strip()10         port = tds[3].text.strip()11         protocol = tds[6].text.strip()12         if protocol == 'HTTP':13             httpResult = 'http://' + ip + ':' + port14         elif protocol =='HTTPS':15             httpsResult = 'https://' + ip + ':' + port

由于Requests是可以直接在访问时候加上proxies的,所以我直接得到的格式使用的是proxies中的格式,requests库文档中,添加代理的格式如下:

import requestsproxies = {  "http": "http://10.10.1.10:3128",  "https": "http://10.10.1.10:1080",}requests.get("http://example.org", proxies=proxies)

 测试可以使用http://www.ip.cn测试访问时的本地IP,代码如下:

1 import requests 2 from bs4 import BeautifulSoup 3 import html5lib 4 headers = { 5 "user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36", 6 } 7 proxies ={ 8     "http":'http://122.193.14.102:80', 9     "https":"http://120.203.18.33:8123"10 }11 r = requests.get('http://www.ip.cn',headers=headers,proxies=proxies)12 content = r.text13 ip=re.search(r'code.(.*?)..code',content)14 print (ip.group(1))

上面的代理需要根据自己实际可用代理替换。

参考链接:http://docs.python-requests.org/zh_CN/latest/user/advanced.html

               http://www.oschina.net/code/snippet_2463131_51169

转载于:https://www.cnblogs.com/rockwall/p/5129670.html

你可能感兴趣的文章
IO流的登录与注册
查看>>
《JavaScript高级程序设计》笔记(1):<script>元素
查看>>
图标字体 VS 雪碧图——图标字体应用实践
查看>>
CORS 跨域
查看>>
kafka 单机配置
查看>>
redis maxheap 51200000
查看>>
jquery .On()绑定事件的触发机制
查看>>
jQuery EasyUI table表单数据绑定与数据交互
查看>>
使用PostThreadMessage在Win32线程间传递消息
查看>>
file_get_contents高級用法
查看>>
【温故知新】c#异步编程模型(APM)--使用委托进行异步编程
查看>>
实现301重定向常用的七种方法
查看>>
leetcode437
查看>>
leetcode974
查看>>
leetcode1099
查看>>
JAVA RPC(二)序列化协议杂谈
查看>>
jQuery中的文档处理
查看>>
JProfiler 8(一个很好的java性能监控工具) 下载和注册码
查看>>
微信小程序wx.chooseImage和wx.previewImage的综合使用(图片上传不限制最多张数)...
查看>>
SQL SERVER数据库日常使用总结
查看>>