爬虫使用代理ip
2017-01-23 12:56
537 查看
获得代理IP的网站:
http://www.xicidaili.com/
验证代理是否可用的方式之一:
如何使用代理:一 使用requests:
二 使用 urllib:
三 使用urllib2的ProxyHandler模块:
四 使用urllib2的request模块:
五:使用httplib:
http://www.xicidaili.com/
验证代理是否可用的方式之一:
globalUrl = "http://ip.chinaz.com/getip.aspx"
如何使用代理:一 使用requests:
import requests
ip = "http://" + i[0]+":"+i[1] ipdict = {"http":ip} requests.get(globalUrl,headers = header,proxies = ipdict,timeout = 3).text
二 使用 urllib:
import urllib
ip = "http://" + i[0]+":"+i[1] ipdict = {"http":ip} try: print urllib.urlopen(globalUrl,proxies=ipdict).read() except Exception,e: print "%s can not use" % ip
三 使用urllib2的ProxyHandler模块:
proxy_info = {'host': i[0], 'port': i[1]} proxy_support = urllib2.ProxyHandler({"http":"http://%(host)s:%(port)s" % proxy_info}) opener = urllib2.build_opener(proxy_support) urllib2.install_opener(opener) request = urllib2.Request(globalUrl,headers= header) try: print urllib2.urlopen(request,timeout = 3).read() except Exception,e: print "%s can not use" % proxy_info["host"] 如果需要验证:
proxy_info = {"host": "xxx", "port": "xxx", "user": "xxx", "pass": "xxx"} proxy_support = urllib2.ProxyHandler({"http":"http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
四 使用urllib2的request模块:
ip = i[0] + ":" + i[1] request = urllib2.Request(globalUrl,headers =header) request.set_proxy(ip,"http") try: print urllib2.urlopen(request,timeout=5).read() except Exception,e: print "%s can not use" % ip
五:使用httplib:
conn = httplib.HTTPConnection(i[0],i[1]) try: conn.connect() conn.request("GET",globalUrl,headers=header) response = conn.getresponse() print response.read() except: print "%s can not use" % i[0]
相关文章推荐
- Python 爬虫入门(二)—— IP代理使用
- python3使用代理ip伪装爬虫访问网站
- python3实现网络爬虫(7)-- 使用ip代理抓取网页
- 鱼c笔记——Python爬虫(三):使用User Agent和代理IP隐藏身份
- 网络爬虫01: Urllib2库使用代理IP
- 如何使用ip代理爬虫
- [Python爬虫] 使用urllib2库检测代理ip是否可用,并使用代理提交GET、POST请求
- Python 爬虫之使用代理ip
- Python 爬虫入门(二)—— IP代理使用
- Python3 爬虫使用User Agent和代理IP隐藏身份
- Python 爬虫入门—— IP代理使用
- 【Python3.6爬虫学习记录】(十一)使用代理IP及用多线程测试IP可用性--刷访问量
- 使用python为爬虫获取免费代理ip
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- 使用阻塞队列爬取代理ip实现爬虫
- Jsoup使用代理ip爬虫
- Python 爬虫入门(二)—— IP代理使用 - 不剃头的一休哥 - 博客园
- Python3网络爬虫之使用User Agent和代理IP隐藏身份
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- httpclient使用代理ip