74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
|
|
"""
|
||
|
|
快速测试代理是否生效
|
||
|
|
"""
|
||
|
|
import asyncio
|
||
|
|
from playwright.async_api import async_playwright
|
||
|
|
|
||
|
|
|
||
|
|
async def quick_test():
|
||
|
|
"""快速测试代理"""
|
||
|
|
# 配置代理
|
||
|
|
proxy = {
|
||
|
|
"server": "http://210.51.27.194:50001",
|
||
|
|
"username": "hb6su3",
|
||
|
|
"password": "acv2ciow"
|
||
|
|
}
|
||
|
|
|
||
|
|
print("\n" + "="*60)
|
||
|
|
print(f"测试代理: {proxy['server']}")
|
||
|
|
print("="*60)
|
||
|
|
|
||
|
|
playwright = await async_playwright().start()
|
||
|
|
|
||
|
|
# 启动浏览器(带代理)
|
||
|
|
browser = await playwright.chromium.launch(
|
||
|
|
headless=False,
|
||
|
|
proxy=proxy
|
||
|
|
)
|
||
|
|
|
||
|
|
context = await browser.new_context()
|
||
|
|
page = await context.new_page()
|
||
|
|
|
||
|
|
# 监听所有请求,查看是否通过代理
|
||
|
|
def on_request(request):
|
||
|
|
print(f"\n[请求] {request.method} {request.url}")
|
||
|
|
# 打印请求头中的代理信息
|
||
|
|
headers = request.headers
|
||
|
|
if 'proxy-authorization' in headers:
|
||
|
|
print(f" [代理认证] 包含代理认证头")
|
||
|
|
|
||
|
|
page.on("request", on_request)
|
||
|
|
|
||
|
|
print("\n[步骤1] 访问IP查询网站...")
|
||
|
|
try:
|
||
|
|
await page.goto('https://httpbin.org/ip', timeout=30000)
|
||
|
|
ip_info = await page.locator('body').inner_text()
|
||
|
|
print(f"\n当前IP信息:\n{ip_info}")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"访问失败: {e}")
|
||
|
|
|
||
|
|
print("\n[步骤2] 访问小红书创作者中心...")
|
||
|
|
try:
|
||
|
|
await page.goto('https://creator.xiaohongshu.com/login', timeout=60000)
|
||
|
|
print(f"页面URL: {page.url}")
|
||
|
|
|
||
|
|
if 'captcha' in page.url:
|
||
|
|
print("⚠️ 触发验证页面")
|
||
|
|
else:
|
||
|
|
print("✅ 正常访问登录页")
|
||
|
|
|
||
|
|
await asyncio.sleep(3)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"访问失败: {e}")
|
||
|
|
|
||
|
|
await browser.close()
|
||
|
|
await playwright.stop()
|
||
|
|
|
||
|
|
print("\n" + "="*60)
|
||
|
|
print("测试完成")
|
||
|
|
print("="*60)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
asyncio.run(quick_test())
|