python3使用代理ip访问指定网站
2016-12-06 20:54
721 查看
首先要找一个可以提供代理ip的网站,然后爬下网站上的ip地址和端口号。最后用爬取出来的ip做代理访问指定网站。
关键地方我用红色箭头标注出来了。分页解析代码如下
head是模仿浏览器请求。将最后解析出来ip和端口号的结果放在proxy里面。然后开始用代理访问指定网站。
proxy_handler = urllib.request.ProxyHandler({'http': proxy_dict})
opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(opener)
req = urllib.request.Request(url="http://blog.csdn.net/u013692888/article/details/52714103", headers=header)
urllib.request.urlopen(req)源码地址https://github.com/Ahuanghaifeng/python3-ip
关键地方我用红色箭头标注出来了。分页解析代码如下
def getProxyIp(): proxy = [] for i in range(1, 3): print(i) header = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Ubuntu Chromium/44.0.2403.89 ' 'Chrome/44.0.2403.89 ' 'Safari/537.36'} req = urllib.request.Request(url='http://www.xicidaili.com/nt/{0}'.format(i), headers=header) r = urllib.request.urlopen(req) soup = BeautifulSoup(r,'html.parser',from_encoding='utf-8') table = soup.find('table', attrs={'id': 'ip_list'}) tr = table.find_all('tr')[1:] #解析得到代理ip的地址,端口,和类型 for item in tr: tds = item.find_all('td') temp_dict = {} kind = "{0}:{1}".format(tds[1].get_text().lower(), tds[2].get_text()) proxy.append(kind) return proxy
head是模仿浏览器请求。将最后解析出来ip和端口号的结果放在proxy里面。然后开始用代理访问指定网站。
proxy_handler = urllib.request.ProxyHandler({'http': proxy_dict})
opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(opener)
req = urllib.request.Request(url="http://blog.csdn.net/u013692888/article/details/52714103", headers=header)
urllib.request.urlopen(req)源码地址https://github.com/Ahuanghaifeng/python3-ip
相关文章推荐
- python使用代理ip访问网站的实例
- python使用代理ip访问网站
- 使用iptables封指定IP访问网站的方法
- python3使用代理ip伪装爬虫访问网站
- 下载指定代理文件调用IE使用代理访问指定网站的代码
- python3使用多代理访问网站
- python 使用代理访问网站
- python3使用多代理访问网站
- [Golang]使用自建代理访问指定网站
- python3使用多代理访问网站
- python使用代理访问网站
- python3使用多代理访问网站
- nginx中对指定的IP不使用密码对区域外IP使用密码访问
- 通过ASP禁止指定IP和只允许指定IP访问网站的代码
- 使用squid代理后某些网站无法访问的解决办法(3.1.7版本)
- ddnslib.py - 更新adsl用户外网ip到动态域名网站no-ip.com,并发送新ip到指定邮箱的python脚本
- 在apache中设置禁止使用IP访问网站的方法
- 使用Apache的Proxy模块实现对被代理网站的访问
- Tomcat 发布部署jsp网站—-使用自己的ip访问jsp网站
- ubuntu无线连接使用ip可以访问网站,但是使用域名却不可以访问的解决办法