Apify Discord Mirror

Updated 5 months ago

Function not executing for a reason.

At a glance
The community member posted an async Python script that scrapes emails from a list of websites. The script works fine in the community member's local development environment, but they are facing issues when trying to integrate it with the Apify platform. A community member suggested putting the entire code within the Apify Actor context manager, and provided a link to the Apify documentation on Actor lifecycles and alternatives. The community member acknowledged that they had already figured this out. Another community member congratulated the original poster on advancing to level 2.
Useful resources
Plain Text
async def main():


    async with Actor:
        input_data = await Actor.get_input() 
        urls = input_data['websites']
        print(urls)




    async with aiohttp.ClientSession() as session:
        tasks = [fetch_emails(session, website) for website in urls]
        results = await asyncio.gather(*tasks)
        print('here')
    dataset = apify_client.dataset()
    for email in website_list:
        print(email)
        dataset.push_data({"email": email})

    print(website_list)
    print(f"tried to scrape {len(urls)}")
    print(f"there is {invalid_url}")
    print(f'successfully scraped {len(website_list)} emails ! ')

asyncio.run(main())
the
Plain Text
async with Actor:
        input_data = await Actor.get_input() 
        urls = input_data['websites']
        print(urls)
does print the url. But after that, the code stops. The scripts work fine in vsc. I'm trying to integrate it with apify but I'm facing some issues.
R
K
A
3 comments
hey, try to put the whole code in the Actor context manager: (under async with Actor:). You can read more about Actor lifecycles and alternatives here: https://docs.apify.com/sdk/python/docs/concepts/actor-lifecycle
Already figured that out but ty anyways
just advanced to level 2! Thanks for your contributions! πŸŽ‰
Add a reply
Sign up and join the conversation on Discord