网络爬虫笔记【3】 使用代理防止本机 IP 被封
2018-10-18 20:55
1001 查看
使用代理是对抗反爬虫机制的常用做法。很多网站会检测某一段时间某个外来 IP 地址对服务器的访问次数等信息。如果访问次数或方式不符合安全策略,就会禁止该外来 IP 对服务器的访问。所以,爬虫设计者可以用一些代理服务器,使自己真实 IP 地址被隐藏,免于被禁止。
urllib 中使用 ProxyHandler 来设置代理服务器的使用
网络上通常有两类代理:免费代理、收费代理。免费代理可以通过百度/Google 搜索,或从以下网站查找:西刺免费代理IP、快代理免费代理、Proxy360代理、全网代理IP ...
- 免费开放代理一般会有好多人在使用,而且代理有寿命短、速度慢、匿名度不高、HTTP/HTTPS支持不稳定等缺点(正所谓,免费没好货)
- 专业爬虫工程师或爬虫公司会使用高质量的私密代理,这些代理通常要找专门的代理供应商购买,再通过用户名/密码授权使用(正所谓,舍不得孩子套不到狼)
可以组织一个代理列表,在一定时间策略下,随机使用,免于被 server 禁止访问。
[code]# 使用代理 # demo 1 :使用 ProxyHandler 通过指定一个免费代理访问目标网站 import urllib.request request = urllib.request.Request('http://www.baidu.com') # 例子里是找的免费代理,说不准什么时候就失效了 proxy_support = urllib.request.ProxyHandler({'http':'210.1.58.212:8080'}) opener = urllib.request.build_opener(proxy_support) response = opener.open(request) print(response.read().decode('utf-8'))
[code]# 使用代理 # demo 2 :使用有身份认证的代理 import urllib.request # 这里的用户名和密码以及代理都是瞎填的,用的时候自行换能用的用 # 流程就是这么个流程,办法就是这么个办法。理解万岁。 username = 'leo' password = 'leo' proxydict = {'http':'106.185.26.199:25'} proxydict['http'] = username + ':' + password + '@' + proxydict['http'] httpWithProxyHandler = urllib.request.ProxyHandler(proxydict) opener = urllib.request.build_opener(httpWithProxyHandler) request = urllib.request.Request('http://www.baidu.com') resp = opener.open(request) print(resp.read().decode('utf-8'))
[code]# 使用代理 # demo 3 :使用 urllib 推荐的做法改进上述过程 username = 'leo' password = 'leo' proxyserver = {'106.185.26.199:25'} # 1. 构建一个密码管理对象,用来保存需要处理的用户名和密码 passwordMgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() # 2. 添加用户信息,第一个参数 realm 是与远程服务器相关的域的信息,默认为None,可通过 response 头查看 # 后面的三个参数分别为:代理服务器,用户名,密码 passwordMgr.add_password(None, proxyserver, username, password) # 3. 构建一个代理基础用户名/密码验证的 Handler 对象,参数为密码管理对象 proxyauth_handler = urllib.request.ProxyBasicAuthHandler(passwordMgr) # 4. 通过 build_opener() 定义 opener 对象 opener = urllib.request.build_opener(proxyauth_handler) # 5. 构造请求 request request = urllib.request.Request('http://www.baidu.com') # 6. 使用自定义 opener 发送请求 response = opener.open(request) # 7. 打印响应内容 print(response.read().decode('utf-8'))
[code]# 使用代理 # demo 4 :使用从 http://www.goubanjia.com/,www.kuaidaili.com/dps 获取的代理列表 # 可以使用快代理在线测试代理可行性 import random # 10月18日 从快代理网站上找的免费代理。 proxylist = [ {'http':'210.1.58.212:8080'}, {'http':'106.185.26.199:25'}, {'http':'124.206.192.210:38621'}, {'http':'222.249.224.61:48114'}, {'http':'115.218.217.184:9000'}, {'http':'183.129.244.17:10010'}, {'http':'120.26.199.103:8118'}, ] def randomTryProxy(retry): ''' Function : choose a proxy from the proxy list RANDOMLY! retry : number of retry ''' # 策略 1 随机选 try: proxy = random.choice(proxylist) print('Try %s : %s' %(retry, proxy)) httpProxyHandler = urllib.request.ProxyHandler(proxy) opener = urllib.request.build_opener(httpProxyHandler) request = urllib.request.Request('http://www.baidu.com') response = opener.open(request,timeout = 5) print('Worked !') except: print('Connect error:Please retry') if retry > 0: randomTryProxy(retry-1) def inorderTryProxy(proxy): ''' Function : choose a proxy from the proxy list RANDOMLY! retry : index of proxy ''' # 策略 2 :依次尝试选择 try: print('Try %s ' %(proxy)) httpProxyHandler = urllib.request.ProxyHandler(proxy) opener = urllib.request.build_opener(httpProxyHandler) request = urllib.request.Request('http://www.baidu.com') response = opener.open(request,timeout = 5) print('Worked !') except: print('Connect error:Please retry') if __name__ == '__main__': # 随机筛选适合代理列表中大部分能用的情况 randomTryProxy(5) print('--'*20) # 依次尝试适合代理列表中大部分不可用的情况 for p in proxylist: inorderTryProxy(p)
[code]运行结果: Try 5 : {'http': '115.218.217.184:9000'} Connect error:Please retry Try 4 : {'http': '115.218.217.184:9000'} Connect error:Please retry Try 3 : {'http': '222.249.224.61:48114'} Connect error:Please retry Try 2 : {'http': '210.1.58.212:8080'} Worked ! ---------------------------------------- Try {'http': '210.1.58.212:8080'} Worked ! Try {'http': '106.185.26.199:25'} Connect error:Please retry Try {'http': '124.206.192.210:38621'} Connect error:Please retry Try {'http': '222.249.224.61:48114'} Connect error:Please retry Try {'http': '115.218.217.184:9000'} Connect error:Please retry Try {'http': '183.129.244.17:10010'} Connect error:Please retry Try {'http': '120.26.199.103:8118'} Connect error:Please retry阅读更多
相关文章推荐
- 网络爬虫01: Urllib2库使用代理IP
- 鱼c笔记——Python爬虫(三):使用User Agent和代理IP隐藏身份
- 笔记(二)网络爬取的高级技巧:使用代理和反爬虫机制
- python3实现网络爬虫(7)-- 使用ip代理抓取网页
- Python3网络爬虫之使用User Agent和代理IP隐藏身份
- Python 爬虫入门(二)—— IP代理使用
- Python 爬虫入门—— IP代理使用
- Python 爬虫入门(二)—— IP代理使用
- 网络爬虫爬取全国省市区(动态ip代理的获取,实现对ip限制的突破)
- python3使用代理ip伪装爬虫访问网站
- 【Python3.6爬虫学习记录】(十一)使用代理IP及用多线程测试IP可用性--刷访问量
- 如何使用ip代理爬虫
- 【笔记】3、初学python3网络爬虫——urllib库的使用
- 【使用JSOUP实现网络爬虫】清理HTML-消除不受信任的html (来防止xss攻击)
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- 使用阻塞队列爬取代理ip实现爬虫
- Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫 从Apache2.2升级到Apache2.4后,发现原来用来限制部分I
- 爬虫使用代理ip
- 【使用JSOUP实现网络爬虫】清理HTML-消除不受信任的html (来防止xss攻击)
- Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫