raena-crawler-engine/hasaki_crawler_engine/test3.py

35 lines
1.5 KiB
Python

import asyncio
from playwright.async_api import async_playwright
async def capture_api_response(url):
async with async_playwright() as p:
browser = await p.chromium.launch()
context = await browser.new_context(user_agent="Mozilla/5.0 (iPhone; CPU iPhone OS 13_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")
page = await context.new_page()
async def capture_and_retry():
response = None
retry_count = 0
while not response and retry_count < 3: # Retry up to 3 times
try:
await page.goto(url)
response = await page.expect_response(lambda resp: "wap/v2/product/detail" in resp.url)
if not response:
print(f"No API response received. Retrying...")
retry_count += 1
await asyncio.sleep(5) # Retry after 5 seconds
except Exception as e:
print(f"Error occurred: {e}")
retry_count += 1
await asyncio.sleep(5) # Retry after 5 seconds
if response:
print(f"API response captured: {await response.text()}")
# Handle the API response here
else:
print("No API response received after multiple attempts.")
await capture_and_retry()
asyncio.run(capture_api_response("https://hasaki.vn/san-pham/son-duong-moi-khong-mau-dhc-ho-tro-giam-tham-moi-1-5g-6710.html"))