Search code examples
pythonasynchronouspython-requests-html

Requests-HTML "You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly."


I need to run javascript on a website (the image search results page of aliseeks.com) and I don't want to use selenium so I am using Requests-HTML. Unfortunately Requests-HTML tells me I need to run an asynchronous session for some reason, which I have never done before. When I try, however, I get the following error:

"You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly."

Here are the relevant parts of my code:

asession = AsyncHTMLSession()

@async_to_sync
async def extract_links(url, headers):

    response = await asession.get(url)

    await response.html.arender()

    print(response.html.raw_html)

#searches AliSeeks
def search(file, headers):

    response = requests.post(url='https://api.aliseeks.com/upload/image', files = file, headers = headers)

    to_str = response.content.decode('utf-8')

    to_dict = ast.literal_eval(to_str)

    #assemble the URL
    url = 'https://www.aliseeks.com/search/image?fskey=' + to_dict['key'] + '&site=ali'

    extract_links(url, headers)

Why is this happening? Are there any alternatives to Requests-HTML that are high performing and simple?


Solution

  • After endless digging around the internet I found out it's not the code. It's Spyder, which is what I was using. I changed to PyCharm and it immediately worked. No idea why