python获取代理IP并测试是否可用
2024-10-19 04:32:29
# coding: utf-8 import urllib2
import re
import time def getDL(page):
url = 'http://www.xicidaili.com/nt/{}'.format(page)
header = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
} req=urllib2.Request(url, headers=header)
res=urllib2.urlopen(req)
html=res.read() srclist=re.findall(r'<tr class=(.|\n)*?<td>(\d+\.\d+\.\d+\.\d+)</td>(.|\n)*?<td>(\d+)</td>(.|\n)*?<td>(HTTP|HTTPS)</td>', html)
xlist = []
for item in srclist:
xlist.append((item[5],item[1],item[3]))
return xlist def testDL(ipstr):
proxy= urllib2.ProxyHandler({'http':"{}:{}".format(ipstr[1], ipstr[2])})
opener=urllib2.build_opener(proxy)
urllib2.install_opener(opener) try:
testUrl = 'http://httpbin.org/ip'
testUrl = 'http://2017.ip138.com/ic.asp'
req=urllib2.Request(testUrl)
res=urllib2.urlopen(req).read()
print "********************* √ {} -- {}".format(ipstr, res) with open("ok.txt","a") as f:
f.write("{} {} {}\n".format(ipstr[0], ipstr[1], ipstr[2]))
f.close()
except Exception as e:
print "******** ×, {} -- {}".format(ipstr, e)
time.sleep(1) def startTask():
for page in xrange(5):
list=getDL(page+1)
for item in list:
testDL(item) if __name__ == '__main__':
startTask()
最新文章
- 计算机维修之操作系统的安装(windows、Mac、Linux)
- Hibernate —— 检索策略
- DP:Making the Grade(POJ 3666)
- PHP中变量,常量,超级全局变量小结
- WinForm C#全局错误捕捉处理【整理】
- asp.net Hierarchical Data
- Supporting Multiple Screens 翻译 支持各种屏幕(上)
- android滑动基础篇 TouchView
- Android使用VideoView播放网络视频
- c#自带压缩类实现数据库表导出到CSV压缩文件
- IOS touch event animation 转动的风车
- 老李推荐:第2章4节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之菜单简介
- conda创建py27虚拟环境安装theano(anaconda3)
- webpack4 splitChunksPlugin &;&; runtimeChunkPlugin 配置杂记
- 圈水池 nyoj 78 凸包算法
- 利用Python中SocketServer 实现客户端与服务器间非阻塞通信
- centos安装VirtualBox增强包VBoxGuestAdditions
- 【转】HTTP协议之multipart/form-data请求分析
- Python int 中 add abs 方法
- 前端和后端的数据交互(jquery ajax+python flask+mysql)