日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 爬取西刺免费代理ip 并使用telnetlib.Telnet验证是否有效

發布時間:2023/12/10 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 爬取西刺免费代理ip 并使用telnetlib.Telnet验证是否有效 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近運行使用時間2017.12.01

運行結果正常

運行環境python.27

#coding:utf8 from bs4 import BeautifulSoup import urllib2 import sys reload(sys) import telnetlibdef getProxyList(targeturl="http://www.xicidaili.com/nn/"):# 創建變量查看爬取IP數量countNum=0# 創建打開txt文件proxyFile=open('ip_port.txt','a')# 設置報頭requestHeader={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0"}# 爬取前五頁for page in range(1,5):url=targeturl+str(page)print(url)request=urllib2.Request(url,headers=requestHeader)html_doc=urllib2.urlopen(request).read()# 使用bs4匹配soup=BeautifulSoup(html_doc,"html.parser")trs=soup.find('table',id='ip_list').find_all('tr')for tr in trs[1:]:tds=tr.find_all('td')# 國家if tds[0].find('img') is None:nation='未知'locate='未知'else:nation=tds[0].find('img')['alt'].strip()locate=tds[4].text.strip()ip=tds[1].text.strip()# print(ip)port=tds[2].text.strip()# print(port)anony=tds[4].text.strip()# print(anony)protocol=tds[5].text.strip()# print(protocol)speed=tds[8].text.strip()time=tds[9].text.strip()proxyFile.write('%s|%s|%s|%s|%s|%s|%s|%s\n' % (nation, ip, port, locate, anony, protocol, speed, time))countNum += 1proxyFile.close()return countNum# 驗證代理有效性 def verifyProxyList():inFile = open('ip_port.txt', 'rb')outFile = open('verified.txt', 'w')while True:# 從ip_port.txt文件中把之前爬取的ip和端口取出來驗證ll=inFile.readline().strip()if len(ll) == 0:breakline=ll.strip().split('|')ip=line[1]port=line[2]# 判斷ip是否可用,有效則寫入到verified.txt文件中try:telnetlib.Telnet(ip, port, timeout=2)outFile.write(ll+"\n")print 'success' + ip + portexcept:print 'connect failed'# 關閉文件inFile.close()outFile.close()if __name__=='__main__':proxynum=getProxyList("http://www.xicidaili.com/nn/")print(u"國內高匿:"+str(proxynum))# proxynum = getProxyList("http://www.xicidaili.com/nt/")# print u"國內透明:" + str(proxynum)# proxynum = getProxyList("http://www.xicidaili.com/wn/")# print u"國外高匿:" + str(proxynum)# proxynum = getProxyList("http://www.xicidaili.com/wt/")# print u"國外透明:" + str(proxynum)verify=verifyProxyList()print("降龍十八掌,打完收工")




總結

以上是生活随笔為你收集整理的python 爬取西刺免费代理ip 并使用telnetlib.Telnet验证是否有效的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。