Update: 初学记、佩文韵府 and 五车韵瑞
This commit is contained in:
46
佩文韵府/test_playwright.py
Normal file
46
佩文韵府/test_playwright.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import asyncio
|
||||
from playwright.async_api import async_playwright
|
||||
import urllib.parse
|
||||
import sys
|
||||
|
||||
|
||||
async def main():
|
||||
url = sys.argv[1]
|
||||
proxy_server = "socks5://127.0.0.1:10808"
|
||||
|
||||
async with async_playwright() as p:
|
||||
# Try with proxy
|
||||
browser = await p.chromium.launch(headless=True, proxy={"server": proxy_server})
|
||||
context = await browser.new_context(
|
||||
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
)
|
||||
page = await context.new_page()
|
||||
|
||||
try:
|
||||
print(f"Loading {url} via proxy...")
|
||||
await page.goto(url, timeout=30000, wait_until="domcontentloaded")
|
||||
content = await page.content()
|
||||
print(f"Success! Content length: {len(content)}")
|
||||
except Exception as e:
|
||||
print(f"Error via proxy: {e}")
|
||||
await browser.close()
|
||||
|
||||
# Try without proxy
|
||||
print("Retrying without proxy...")
|
||||
browser = await p.chromium.launch(headless=True)
|
||||
context = await browser.new_context(
|
||||
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
)
|
||||
page = await context.new_page()
|
||||
try:
|
||||
await page.goto(url, timeout=30000, wait_until="domcontentloaded")
|
||||
content = await page.content()
|
||||
print(f"Success! Content length: {len(content)}")
|
||||
except Exception as e2:
|
||||
print(f"Error without proxy: {e2}")
|
||||
|
||||
await browser.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user