多线程代理ip验证(requests)
来源:互联网 发布:网页配色软件 编辑:程序博客网 时间:2024/06/11 20:05
直接上代码:
import requestsfrom queue import Queueimport threadingclass proxy_ip(): # 初始化参数 def __init__(self): self.url = 'http://www.baidu.com/' self.ip_list_queue = Queue() # 创建队列用于存储所有代理 self.proxy_queue = Queue() # 创建存储有效代理 # 获取代理ip def get_ip(self): # 这是我之前采集到代理ip存放在txt中,格式是ip:port with open('./ip.txt') as f: ip_list = f.readlines() for ip in ip_list: ip = ip.strip() # proxy = {"http":"http://" + ip,"https":"http://" + ip,} self.ip_list_queue.put(ip) # 存放到队列 # 验证代理ip def check_ip(self): while True: ip = self.ip_list_queue.get() # 在队列中取proxy try: proxy = {"http": "http://" + ip} requests.get(self.url, proxies=proxy, timeout=1) ip = ip print(ip) except: ip = None finally: self.proxy_queue.put(ip) # 存入队列 self.ip_list_queue.task_done() # 队列计数减一 # 写入可用ip def write_ip(self): while True: ip = self.proxy_queue.get() # 取 if ip is not None: with open('./proxy.txt', 'a') as f: f.write(ip + '\n') self.proxy_queue.task_done() # 减 # 运行主逻辑 def run(self): thread_list = [] t_get_ip = threading.Thread(target=self.get_ip) thread_list.append(t_get_ip) for i in range(30): t_check_ip = threading.Thread(target=self.check_ip) thread_list.append(t_check_ip) t_write_ip = threading.Thread(target=self.write_ip) thread_list.append(t_write_ip) for t in thread_list: t.setDaemon(True) # 守护线程 t.start() for q in [self.ip_list_queue, self.proxy_queue]: q.join() print('运行结束')if __name__ == '__main__': start = proxy_ip() start.run()
多线程学习看我之前的案列
多线程爬虫案例(浅)一
:
阅读全文
0 0
- 多线程代理ip验证(requests)
- requests 使用代理验证
- 抓取代理IP并多线程验证
- requests使用代理ip的方法
- 简单使用requests库爬取Ip代理
- 多线程代理IP池
- python:使用requests,bs4爬取西刺代理并验证
- 代理IP批量验证程序
- 获得代理ip并验证
- C#验证代理Ip地址
- python多线程筛选代理ip
- python3中requests中使用ip代理池随机生成ip
- c# 快速验证代理IP是否有用
- c# 快速验证代理IP是否有用
- C# 验证代理IP的方法
- c# 快速验证代理IP是否有用
- C# 验证代理IP的方法
- c# 快速验证代理IP是否有用
- 101. Symmetric Tree
- mongodb,redis,hbase 三者都是nosql数据库,他们的最大区别和不同定位
- python列表的11种方法
- 图解Nosql(HBase)与传统数据库的区别
- MacBook Pro 2017外接显卡实战——打破Mac不能玩游戏的定律
- 多线程代理ip验证(requests)
- 线性神经网络及其代码实现
- HashMap、LinkedHashMap和TreeMap的实例验证
- HastSet、LinkedHashSet和TreeSet的实例验证
- 队列的Queue接口和Deque接口的方法概述
- EAP(6110)作业系统launchpad之开挂做题
- Tomcat报错 java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener
- 关于无穷小(Infinitesimal)的严格定义
- 亚马逊AWS计算机视觉功能更新:可实时进行脸部识别