Debugging with keystroke using async actor?
Hello - im am testing with the basic template for python:
https://apify.com/templates/python-start
For debugging reasons i would like to output every heading and then waiting for a keystrike using <input("Press")> using the following code code below.
But when i press a key when running the program in the terminal i does not go to the next element.
How can i do this using async so i get every output after a keystroke?
Code:
from future import annotations
from apify import Actor
from bs4 import BeautifulSoup
from httpx import AsyncClient
async def main() -> None:
async with Actor:
actor_input = await Actor.get_input() or {'url': 'https://apify.com/'}
url = actor_input.get('url')
if not url:
raise ValueError('Missing "url" attribute in input!')
async with AsyncClient() as client: Actor.log.info(f'Sending a request to {url}') response = await client.get(url, follow_redirects=True) soup = BeautifulSoup(response.content, 'lxml') headings = [] for heading in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']): heading_object = {'level': heading.name, 'text': heading.text} Actor.log.info(f'Extracted heading: {heading_object}') input("Press!") headings.append(heading_object) await Actor.push_data(headings)
async with AsyncClient() as client: Actor.log.info(f'Sending a request to {url}') response = await client.get(url, follow_redirects=True) soup = BeautifulSoup(response.content, 'lxml') headings = [] for heading in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']): heading_object = {'level': heading.name, 'text': heading.text} Actor.log.info(f'Extracted heading: {heading_object}') input("Press!") headings.append(heading_object) await Actor.push_data(headings)
Apify
Start with Python · Template · Apify
Scrape single page with provided URL with HTTPX and extract data from page's HTML with Beautiful Soup.
3 Replies
@RapidTech1898 just advanced to level 1! Thanks for your contributions! 🎉
Yes, input() in async doesn't work as it should — it blocks everything. To wait for Enter to be pressed, simply wrap input() in await asyncio.to_thread(...), and it will work as it should. Like this: await asyncio.to_thread(input, "Press Enter...").
Hello - thanks for the response -
I tried it this way:
async def main() -> None:
async with Actor: await Actor.set_status_message('Starting actor...') asyncio.to_thread(input, "Press Enter...") But its not stopping in any way with that.
async with Actor: await Actor.set_status_message('Starting actor...') asyncio.to_thread(input, "Press Enter...") But its not stopping in any way with that.