测试代理ip是否工作正常,将正常的代理ip提取出来
from selenium import webdriver
from fake_useragent import UserAgent
def check_proxy(proxy):print("开始测试:"+proxy)chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('--headless')chrome_options.add_argument('--disable‐gpu')chrome_options.add_argument("--disable-blink-features=AutomationControlled")chrome_options.add_argument('--proxy-server=http://' + proxy)chrome_options.add_argument(f"user-agent={UserAgent().random}")try:driver = webdriver.Chrome(options=chrome_options)driver.get("https://www.baidu.com")# You can add additional checks or actions here if neededdriver.quit() # Write usable proxies to ip1.txtwith open('ip1.txt', 'a') as f:f.write(proxy+"\n")print(f"代理 {proxy} 工作正常")except Exception as e:print(f"代理 {proxy} 无法工作")# Test each IP and write usable ones to ip1.txt
usable_proxies = []with open('ip.txt', 'r') as f:for line in f:ip = line.strip()check_proxy(ip)
测试结果