如何选择免费代理ip,主要指标值和基本参数
1、电脑安装python
2、安装requests和bs4两个库
pip install requests bs4
3、通过requests过去网站html代码,bs4引入BeautifulSoup获取到IP相关信息。
代码部分:
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
res = requests.get(url,headers=headers)
res.encoding = "utf-8"
soup = BeautifulSoup(res.text, 'html.parser')
tables = soup.findAll('table')
tab = tables[0]
db = []
for tr in tab.findAll('tr')[1::]:
p = tr.find('td')
c = p.next_sibling.next_sibling
y = c.next_sibling.next_sibling
s = y.next_sibling.next_sibling
t = s.next_sibling.next_sibling
msg = {"省份":p.text,"城市":c.text,"运营商":y.text,"域名":s.text,"状态":t.text}
db.append(msg)
4、使用requests的post去验证IP的有效性
def porxy(ip):
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
try:
res = requests.get('https://998ip.com', proxies={"http":"http://%s"%ip},timeout=2,headers=headers)
res.encoding = "utf-8"
return res.status_code
except:
return '连接超时'
返回状态码为200即为有效IP。