Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dgtlmoon/6f306e321200af521f82843bd4bc9901 to your computer and use it in GitHub Desktop.
Save dgtlmoon/6f306e321200af521f82843bd4bc9901 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
"""
DOES NOT WORK because requests (and requests_cache) are blocking, if you have some ideas..
https://stackoverflow.com/questions/52246796/await-a-method-and-assign-a-variable-to-the-returned-value-with-asyncio
"""
import asyncio
import re
import time
import requests_cache
from playwright.async_api import async_playwright
async def fetcher():
async def handle(route, request):
print("handler called at {} {}".format(str(time.time()), request.url))
session = requests_cache.CachedSession('demo_cache')
response = session.get(request.url, headers=request.headers)
print (response.from_cache)
await route.fulfill(
status=response.status_code,
content_type=response.headers.get('content-type'),
body=response.content,
headers=dict(response.headers))
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp('ws://127.0.0.1:3000')
page = await browser.new_page()
await page.route(re.compile(r"\.(png|jpg|jpeg|css|js|webp|gif|mov|mpg|avi|mpeg|txt|ttf|woff)", re.IGNORECASE), handle)
await page.goto('https://theguardian.co.uk', timeout=300000)
print(f'title:{await page.title()}')
async def main():
print("Calling async")
await fetcher()
print("Running in the asyncio")
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment