python测试代理IP地址
2016-05-30 18:06
423 查看
代码:
使用代理IP地址成功的时候会显示使用的代理IP地址,或者显示timeout以及链接错误。
完毕。
# -*- coding: utf-8 -*- import urllib,urllib2,re from random import choice from scrapy.selector import Selector #特稳定IP 107.151.152.218:80 #http://www.xicidaili.com/wn/测试可用IP '107.151.136.202:80',222.124.130.34:8080,'103.14.196.74:8080','107.151.142.114:80''54.169.238.128:9999' #agent = ['49.113.101.167:8090','113.78.28.205:8090','219.136.31.16:8090','119.131.83.227:8090','221.221.206.208:8090','116.52.16.57:8090','182.109.80.149:9000','59.55.59.41:9000','115.223.201.206:9000'] agent = ['107.151.136.194:80'] proxy= choice(agent) #urllib2.ProxyHandler({'http': 'http://%s/' % proxy}) #content = urllib2.urlopen('http://www.ip.cn/').read()#111.192.249.170 handlers = [urllib2.ProxyHandler({'http': 'http://%s/' % proxy})] opener = urllib2.build_opener(*handlers) #opener = urllib2.build_opener() content = opener.open(urllib2.quote('http://www.ip.cn/', safe=":/"), timeout=30).read() sel = Selector(text=content, type="html") ip = sel.re(u'<code>\s*(.*)\s*</code>') print ip
使用代理IP地址成功的时候会显示使用的代理IP地址,或者显示timeout以及链接错误。
完毕。
相关文章推荐
- python爬虫:常用浏览器的useragent
- python 数据加密以及生成token和token验证
- Python入门:字符串处理
- Python操作MySQL数据库
- windows下安装bpython
- python 常用模块 Top200
- python编码
- 使用Python写一个小小的项目监控
- python3深入学习教程
- 深入理解python中的浅拷贝和深拷贝
- Python爬虫学习系列
- python基础之异常处理
- 关于Python中面向对象
- Python使用struct处理二进制--pack,unpack
- Machine Learning in Action 学习笔记-(1)
- Python2与Python3的区别(三):print
- 解决Python中使用mysqldb的一些问题
- 浅谈Python的文件类型
- python字符串和编码
- python的metaclass