python通过免费代理构建可用代理池

编程入门 行业动态 更新时间:2024-10-25 03:28:19

<a href=https://www.elefans.com/category/jswz/34/1770869.html style=python通过免费代理构建可用代理池"/>

python通过免费代理构建可用代理池

以下为简单示例,后续再进行优化,特作记录!

import requests
import re
import telnetliburl='.php?getnum=100000&isp=0&anonymoustype=4&start=&ports=&export=&ipaddress=&area=2&proxytype=0&api=66ip'
header = {'User-agent':'xxxxxxxxxxx','Connection':'close'}
page_text = requests.get(url=url,headers=header).text
#用于匹配ip:port的正则表达式
p = r'(?:((?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5]))\D+?(6[0-5]{2}[0-3][0-5]|[1-5]\d{4}|[1-9]\d{1,3}|[0-9]))'
#匹配出所有的ip与端口,放入列表
iplist = re.findall(p,page_text)
# 判断爬取的代理ip是否可用
for i in iplist:ip = i[0]+':'+i[1]+'\n'try:# 使用代理ip访问指定网站,能访问说明代理可用# requests.get('', proxies={"http": ip})# 使用telnet测试代理是否可用telnetlib.Telnet(i[0], port=i[1], timeout=3)except:print('connect failed')else:print(ip)with open('ip.txt', 'a+', encoding='utf-8') as fp:fp.write(ip)


更多推荐

python通过免费代理构建可用代理池

本文发布于:2024-02-11 03:35:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1678917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!