# -*- 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