网络上提供代理IP服务的商家是越来越多,但与此同时,选择的增加也无疑给了消费者筛选难度。除了验证服务商的资质,代理IP的有效性也是我们最为关注的问题。在一番挑选之后,终于找到了合适的代理,那么这时,我们就可以开始工作了。
下面分别说说如何代理IP是使用urllib2库提交GET、POST请求的。
如何使用代理IP提交GET请求?
import urllib2
def UseProxyToGet(proxy_ip, proxy_port):
proxy_support = urllib2.ProxyHandler({‘http’: r’http://’ + proxy_ip + ‘:’ + proxy_port})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
try:
url = "“http://httpbin.org/get?a=1&b=2"”
opener.addheaders = [
(‘Cookie’, ‘this is cookie’),
(‘Referer’, ‘this is refere’),
(‘User-Agent’, ‘this is user-agent’),
response = opener.open(url, timeout=5)
html = response.read()
print html
except urllib2.HTTPError as e:
print(‘UseProxyToGet:’, proxy_ip, ’ HTTPError:’, e)
except urllib2.URLError as e:
print(‘UsePro