爬虫代理IP

爬虫使用代理ip

获得代理IP的网站:

http://www.xicidaili.com/

验证代理是否可用的方式之一:

globalUrl = "http://ip.chinaz.com/getip.aspx"

如何使用代理:

一 使用requests:

ip = "http://" + i[0]+":"+i[1]
requests.get(globalUrl,headers = header,proxies = ipdict,timeout = 3).text

二 使用 urllib:   

ip = "http://" + i[0]+":"+i[1]
try:
except Exception,e:
proxy_info = {'host': i[0],
proxy_support = urllib2.ProxyHandler({"http":"http://%(host)s:%(port)s" % proxy_info})
urllib2.install_opener(opener)
try:
except Exception,e:
proxy_info = {"host": "xxx",
"user": "xxx",
proxy_support = urllib2.ProxyHandler({"http":"http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})

四 使用urllib2的request模块:

request = urllib2.Request(globalUrl,headers =header)
try:
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]

转载于:https://www.cnblogs.com/GUIDAO/p/6689642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值