正式2
This commit is contained in:
@@ -62,6 +62,52 @@ class WebTrafficBot:
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
]
|
||||
|
||||
def get_current_ip(self):
|
||||
"""获取当前IP地址"""
|
||||
ip_services = [
|
||||
'https://httpbin.org/ip',
|
||||
'https://api.ipify.org?format=json',
|
||||
'https://ipinfo.io/json',
|
||||
'https://ifconfig.me/ip'
|
||||
]
|
||||
|
||||
for service in ip_services:
|
||||
try:
|
||||
logger.info(f"正在获取IP地址: {service}")
|
||||
response = self.session.get(service, timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
if 'json' in service or 'httpbin' in service or 'ipinfo' in service:
|
||||
try:
|
||||
data = response.json()
|
||||
if 'origin' in data: # httpbin.org
|
||||
ip = data['origin']
|
||||
elif 'ip' in data: # ipify.org 或 ipinfo.io
|
||||
ip = data['ip']
|
||||
else:
|
||||
ip = str(data)
|
||||
except:
|
||||
ip = response.text.strip()
|
||||
else:
|
||||
ip = response.text.strip()
|
||||
|
||||
logger.info(f"✅ 当前IP地址: {ip}")
|
||||
|
||||
# 如果配置了代理,显示代理信息
|
||||
if self.proxy_config:
|
||||
logger.info(f"🌐 代理服务器: {self.proxy_config['host']}:{self.proxy_config['port']}")
|
||||
else:
|
||||
logger.info("🌐 未使用代理,使用本地IP")
|
||||
|
||||
return ip
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"从 {service} 获取IP失败: {e}")
|
||||
continue
|
||||
|
||||
logger.error("❌ 无法获取当前IP地址")
|
||||
return None
|
||||
|
||||
def setup_session(self):
|
||||
"""设置请求会话"""
|
||||
self.session = requests.Session()
|
||||
@@ -83,6 +129,11 @@ class WebTrafficBot:
|
||||
'Connection': 'keep-alive',
|
||||
'Upgrade-Insecure-Requests': '1',
|
||||
})
|
||||
|
||||
# 获取并显示当前IP
|
||||
current_ip = self.get_current_ip()
|
||||
if current_ip:
|
||||
print(f"🌍 当前IP地址: {current_ip}")
|
||||
|
||||
def setup_selenium_driver(self):
|
||||
"""设置Selenium WebDriver"""
|
||||
|
||||
Reference in New Issue
Block a user