爬虫设置代理

来源:互联网 发布:java线程yield 编辑:程序博客网 时间:2024/05/21 07:58

介绍两种方法:requests、urllib2

import urllib2import lxml.htmlimport requestsurl = 'http://www.ip181.com/'proxy = {'http': 'http://111.13.7.42:82'}'''opener = urllib2.build_opener(urllib2.ProxyHandler(proxy))urllib2.install_opener(opener)html = urllib2.urlopen(url).read()'''html = requests.get(url, proxies=proxy).contenttree = lxml.html.fromstring(html)content = tree.cssselect('div.panel-info:nth-child(2) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2)')[0].textprint content
原创粉丝点击