【89ip可用代理收集】

# -*- coding: GBK -*-
import requests
import re
from concurrent.futures import ThreadPoolExecutor

url = "http://api.89ip.cn/tqdl.html?api=1&num=100&port=&address=&isp="

# 目标网站的URL
target_url = "http://www.baidu.com"

def test_proxy(address):
    try:
        # 将代理地址转换为字典
        proxies = {
            "http": "http://" + address,
            "https": "https://" + address,
        }

        # 发送通过代理的GET请求
        response = requests.get(target_url, proxies=proxies, timeout=2)

        # 检查响应状态码
        if response.status_code == 200:
            with open("proxy_results.txt", "a", encoding="utf-8") as file:
                print(f"代理 {address} 可用")
                file.write(f"{address}\n")
        else:
            print(f"代理 {address} 不可用,状态码:{response.status_code}")
    except requests.RequestException as e:
        # 不打印异常信息
        pass

try:
    # 发送请求获取页面内容
    response = requests.get(url)

    # 检查响应状态码
    if response.status_code == 200:

        proxy_info_string = response.text

        # 使用正则表达式提取代理地址
        proxy_pattern = re.compile(r'(\d+\.\d+\.\d+\.\d+:\d+)')
        proxy_addresses = proxy_pattern.findall(proxy_info_string)

        # 使用ThreadPoolExecutor创建线程池
        with ThreadPoolExecutor() as executor:
            # 使用多线程测试代理
            executor.map(test_proxy, proxy_addresses)

    else:
        print(f"请求失败,状态码:{response.status_code}")
except requests.RequestException as e:
    print(f"发生异常:{e}")

QQ群:839758211

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值