本文目录导读:

我来教你如何编写代理IP切换脚本,包含多种实现方式。
基础代理IP切换脚本
Python版本(requests库)
import requests
import random
import time
from itertools import cycle
class ProxyManager:
def __init__(self, proxy_list):
"""
初始化代理管理器
:param proxy_list: 代理列表,格式 ['http://ip:port', ...]
"""
self.proxy_list = proxy_list
self.proxy_cycle = cycle(proxy_list) # 循环使用代理
self.current_proxy = None
def get_next_proxy(self):
"""获取下一个代理"""
self.current_proxy = next(self.proxy_cycle)
return self.current_proxy
def get_random_proxy(self):
"""随机获取一个代理"""
self.current_proxy = random.choice(self.proxy_list)
return self.current_proxy
def test_proxy(self, proxy, timeout=5):
"""测试代理是否可用"""
try:
response = requests.get(
'http://httpbin.org/ip',
proxies={'http': proxy, 'https': proxy},
timeout=timeout
)
if response.status_code == 200:
print(f"代理 {proxy} 有效,IP: {response.json()['origin']}")
return True
except:
print(f"代理 {proxy} 无效")
return False
return False
def filter_working_proxies(self):
"""过滤出有效代理"""
working_proxies = []
for proxy in self.proxy_list:
if self.test_proxy(proxy):
working_proxies.append(proxy)
time.sleep(0.5) # 避免请求过快
return working_proxies
# 使用示例
def main():
# 代理列表(替换为实际代理)
proxy_list = [
'http://proxy1.example.com:8080',
'http://proxy2.example.com:8080',
'http://proxy3.example.com:8080',
# ... 更多代理
]
# 初始化代理管理器
proxy_manager = ProxyManager(proxy_list)
# 过滤有效代理
working_proxies = proxy_manager.filter_working_proxies()
print(f"有效代理: {working_proxies}")
# 使用代理请求
for i in range(10):
proxy = proxy_manager.get_next_proxy() # 或 get_random_proxy()
print(f"使用代理: {proxy}")
try:
response = requests.get(
'https://httpbin.org/ip',
proxies={'http': proxy, 'https': proxy},
timeout=10
)
print(f"请求成功: {response.json()}")
except Exception as e:
print(f"请求失败: {e}")
time.sleep(1) # 适当延迟
if __name__ == "__main__":
main()
带身份验证的代理
import requests
from requests.auth import HTTPProxyAuth
class AuthenticatedProxyManager:
def __init__(self, proxy_list, username=None, password=None):
self.proxy_list = proxy_list
self.username = username
self.password = password
self.auth = None
if username and password:
self.auth = HTTPProxyAuth(username, password)
def make_request(self, url, proxy=None):
"""使用代理发送请求"""
if not proxy:
proxy = random.choice(self.proxy_list)
proxies = {
'http': proxy,
'https': proxy
}
try:
response = requests.get(
url,
proxies=proxies,
auth=self.auth,
timeout=10
)
return response
except Exception as e:
print(f"请求失败: {e}")
return None
# 使用示例
proxy_manager = AuthenticatedProxyManager(
proxy_list=['http://proxy.example.com:8080'],
username='your_username',
password='your_password'
)
response = proxy_manager.make_request('https://httpbin.org/ip')
Selenium WebDriver代理切换
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import random
class SeleniumProxyManager:
def __init__(self, proxy_list):
self.proxy_list = proxy_list
self.driver = None
def create_driver_with_proxy(self, proxy):
"""创建带代理的Chrome浏览器"""
chrome_options = Options()
chrome_options.add_argument(f'--proxy-server={proxy}')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
# 可选:设置用户代理
chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
self.driver = webdriver.Chrome(options=chrome_options)
return self.driver
def switch_proxy(self, new_proxy):
"""切换代理(需要重新启动浏览器)"""
if self.driver:
self.driver.quit()
return self.create_driver_with_proxy(new_proxy)
def random_proxy_request(self, url):
"""使用随机代理访问URL"""
proxy = random.choice(self.proxy_list)
print(f"使用代理: {proxy}")
driver = self.create_driver_with_proxy(proxy)
try:
driver.get(url)
return driver.page_source
finally:
driver.quit()
# 使用示例
proxy_list = [
'http://proxy1:8080',
'http://proxy2:8080',
'http://proxy3:8080'
]
selenium_manager = SeleniumProxyManager(proxy_list)
driver = selenium_manager.create_driver_with_proxy('http://proxy1:8080')
driver.get('https://www.baidu.com')
自动获取和轮换代理
import requests
from bs4 import BeautifulSoup
import re
class ProxyScraper:
def __init__(self):
self.proxies = []
def get_free_proxies_from_web(self):
"""从免费代理网站获取代理"""
urls = [
'https://www.sslproxies.org/',
'https://free-proxy-list.net/',
# 更多代理源
]
for url in urls:
try:
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(response.text, 'html.parser')
# 解析代理表格
proxy_table = soup.find('table', id='proxylisttable')
if proxy_table:
for row in proxy_table.tbody.find_all('tr'):
columns = row.find_all('td')
if len(columns) >= 2:
ip = columns[0].text.strip()
port = columns[1].text.strip()
proxy = f'http://{ip}:{port}'
self.proxies.append(proxy)
except Exception as e:
print(f"获取代理失败: {e}")
return self.proxies
def validate_proxies(self, max_workers=10):
"""验证代理有效性"""
from concurrent.futures import ThreadPoolExecutor, as_completed
working_proxies = []
def test_proxy(proxy):
try:
response = requests.get(
'http://httpbin.org/ip',
proxies={'http': proxy, 'https': proxy},
timeout=5
)
if response.status_code == 200:
return proxy
except:
pass
return None
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [executor.submit(test_proxy, proxy) for proxy in self.proxies]
for future in as_completed(futures):
result = future.result()
if result:
working_proxies.append(result)
return working_proxies
# 自动化代理获取和使用
scraper = ProxyScraper()
all_proxies = scraper.get_free_proxies_from_web()
print(f"获取到 {len(all_proxies)} 个代理")
working_proxies = scraper.validate_proxies()
print(f"有效代理: {len(working_proxies)} 个")
# 使用工作代理
proxy_manager = ProxyManager(working_proxies)
高级特性:自动重试和轮换
class AdvancedProxyManager:
def __init__(self, proxy_list, max_retries=3):
self.proxy_list = proxy_list
self.max_retries = max_retries
self.failed_proxies = set()
def request_with_retry(self, url, max_attempts=None):
"""带重试的请求"""
if max_attempts is None:
max_attempts = len(self.proxy_list)
for attempt in range(max_attempts):
# 获取可用代理
available_proxies = [p for p in self.proxy_list if p not in self.failed_proxies]
if not available_proxies:
print("所有代理都已失效")
return None
proxy = random.choice(available_proxies)
try:
response = requests.get(
url,
proxies={'http': proxy, 'https': proxy},
timeout=10
)
if response.status_code == 200:
print(f"第 {attempt + 1} 次尝试成功,使用代理: {proxy}")
return response
else:
print(f"代理 {proxy} 返回状态码: {response.status_code}")
self.failed_proxies.add(proxy)
except Exception as e:
print(f"代理 {proxy} 失败: {e}")
self.failed_proxies.add(proxy)
time.sleep(1) # 重试前等待
return None
def reset_failed_proxies(self):
"""重置失败代理记录"""
self.failed_proxies.clear()
print("已重置失败代理记录")
# 使用示例
advanced_manager = AdvancedProxyManager(proxy_list)
response = advanced_manager.request_with_retry('https://httpbin.org/ip')
使用建议
- 代理来源:可以从付费代理服务、免费代理网站或自建代理池获取
- 验证机制:每次使用前测试代理可用性
- 速度限制:添加适当的延迟避免被封锁
- 错误处理:实现重试机制和备用方案
- 安全考虑:不要在代理请求中传输敏感信息
选择合适的代理IP脚本取决于你的具体需求,如是否需要身份验证、是否使用Selenium等。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播