目标网址 http://lab.crossincode.com/proxy/
目的:获取稳定的ip,这是一个'Crossin的编程教室'的编程公众号提供的一个ip池,我相当于是二次提取.但是可用性够高,粗算了一下ip可用性70%左右,算是很好的池子了.以下是我的IP提取代码,file的位置自己改吧.
import requests
from parsel import Selector
file='F:/Python/精品收藏代码/proxies_my.json'
f=open(file,'w+')#覆盖用w+,新增用a+
url='http://lab.crossincode.com/proxy/'
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'}
r=requests.get(url,headers=header)
r.encoding='utf-8'#网页上提供的charset是utf-8
html=Selector(text=r.text)#Selecotor这个模块非常好用
response=html.xpath("//table[@class='table table-bordered proxy-index-table']/tr")
print(len(response))
def proxydownload(s):
try:
print(s)
f.write(s+"\n")
except Exception as e:
print(e)
pass
n=1
for i in range(len(response)-1):
types=html.xpath("//table[@class='table table-bordered proxy-index-table']/tr/td[4]/text()")[i].extract().strip()
if types:#选择https类型
print(n)
ip=html.xpath("//table[@class='table table-bordered proxy-index-table']/tr/td[1]/text()")[i].extract().strip()
api=html.xpath("//table[@class='table table-bordered proxy-index-table']/tr/td[2]/text()")[i].extract().strip()
types=html.xpath("//table[@class='table table-bordered proxy-index-table']/tr/td[4]/text()")[i].extract().strip()
types=types[-5:]
ips=ip+":"+api
s='{"'+types+'"'+":"+'"'+types+'://'+ips+'"}'
n+=1
proxydownload(s)
else:
pass
f.close()
print('运行完成')
获得的ip如图
_T1P@LI%EJ8)VP3Y_%JR85H.png
网友评论