【python爬虫】搭建自己的免费IP代理池(下)——【多线程验证代理IP】

功能

  • 载入已经爬取的IP代理txt文件,多线程对其进行验证。
  • 将响应快的代理IP保存为txt文件。

效果图

在这里插入图片描述
在这里插入图片描述

代码

# -*- coding: utf-8 -*-
# @Time    : 2021/3/19 10:30
# @Author  : AA8j
# @FileName: CheckIP.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/qq_44874645
import requests
import threading


def check_proxy(check_ip: dict):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_16_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    }
    try:
        # 6秒内通过测试
        r = requests.get('http://www.baidu.com', headers=headers, proxies=check_ip, timeout=time_out)
        if r.status_code == 200:
            # 验证成功
            return True
    except:
        # 验证失败
        return False


def read_ip():
    ip_list = []
    try:
        print('正在读取ip...', end='')
        with open('save/ips.txt', 'r') as f:
            for ip in f:
                ip_dic = eval(ip.split('\n')[0])
                ip_list.append(ip_dic)
        print(f'读取成功!共读取{len(ip_list)}条。')
        return ip_list
    except Exception as e:
        print('读取失败!')
        raise e


def save_success_ip(ip_lsit):
    try:
        with open('save/success_ips.txt', 'w') as f:
            for i in ip_lsit:
                f.write(str(i) + '\n')
        print(f'保存成功!共保存{len(ip_lsit)}条。')
    except Exception as e:
        print('保存失败!', e)


# 创建一个线程类
class MyThread(threading.Thread):
    def __init__(self, proxy_ip: dict):
        super().__init__()
        self.proxy_ip = proxy_ip

    def run(self):
        # 如果通过了检测,则加入可用代理列表
        with threading_max:
            Flag = check_proxy(self.proxy_ip)
            # 防止同时输出
            with lock:
                if Flag:
                    SuccessProxyList.append(self.proxy_ip)
                    print(threading.current_thread().name.ljust(12), str(self.proxy_ip).ljust(35),
                          '--------验证成功!√'.ljust(15))
                else:
                    print(threading.current_thread().name.ljust(12), str(self.proxy_ip).ljust(35),
                          '--------验证失败!'.ljust(15))


if __name__ == '__main__':
    # 读取已经爬取的IP
    IpList = read_ip()
    # 多线程验证代理
    # 成功代理的列表
    SuccessProxyList = []
    # 最大并发线程数量
    max_num = 30
    threading_max = threading.Semaphore(max_num)
    # 超时时间
    time_out = 6
    # 同步锁,防止同时输出
    lock = threading.Lock()

    # 创建线程池
    ThreadList = []
    for i in IpList:
        thread = MyThread(i)
        ThreadList.append(thread)
    print(f'开始验证...(线程数:{max_num}、超时时间:{time_out})')
    for i in ThreadList:
        i.start()

    for i in ThreadList:
        i.join()

    save_success_ip(SuccessProxyList)

项目地址:https://github.com/AA8j/FreeProxy

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值