Async workflow to get Home Page and ondemand.s response
masterTo perform an asynchronous scrape/request flow using httpx:
- Initialize an
httpx.AsyncClientwith headers fromgenerate_headers(). - Use
await handle_x_migration_async(session=session)to get the home page response. - Use
get_ondemand_file_url(response=home_page_response)to find theondemand.sURL. - Await the
session.get()for theondemand.sfile.
Troubleshooting: If you encounter a "Couldn't get KEY_BYTE indices" error, try passing the raw response text (ondemand_file.text) instead of the BeautifulSoup object to ClientTransaction.
import bs4
import httpx
from x_client_transaction.utils import generate_headers, handle_x_migration_async, get_ondemand_file_url
# 1. Initialize Async Session
session = httpx.AsyncClient(headers=generate_headers())
# 2. Get Home Page
home_page_response = await handle_x_migration_async(session=session)
# 3. Get ondemand.s
ondemand_file_url = get_ondemand_file_url(response=home_page_response)
ondemand_file = await session.get(url=ondemand_file_url)
# 4. Parse
ondemand_file_response = bs4.BeautifulSoup(ondemand_file.content, 'html.parser')
# Alternative if error occurs:
# ondemand_file_response = ondemand_file.text