我用Python写了一个脚本,从网页中获取一些信息。如果脚本不在异步IO中运行,代码本身是完美的。然而,由于我的脚本是同步运行的,我希望将其转换为异步过程,以便在最短时间内完成任务,提供最佳性能并且不会阻塞。由于我从未使用过异步IO库,我非常困惑如何使它正常工作。我试图将我的脚本嵌入到异步IO过程中,但似乎行不通。如果有人能帮助我完成这个问题,我将非常感激。提前致谢。这是我错误的代码:
import requests ; from lxml import html
import asyncio
link = "http://quotes.toscrape.com/"
async def quotes_scraper(base_link):
response = requests.get(base_link)
tree = html.fromstring(response.text)
for titles in tree.cssselect("span.tag-item a.tag"):
processing_docs(base_link + titles.attrib['href'])
async def processing_docs(base_link):
response = requests.get(base_link).text
root = html.fromstring(response)
for soups in root.cssselect("div.quote"):
quote = soups.cssselect("span.text")[0].text
author = soups.cssselect("small.author")[0].text
print(quote, author)
next_page = root.cssselect("li.next a")[0].attrib['href'] if root.cssselect("li.next a") else ""
if next_page:
page_link = link + next_page
processing_docs(page_link)
loop = asyncio.get_event_loop()
loop.run_until_complete(quotes_scraper(link))
loop.close()
执行后,在控制台上看到的是:
RuntimeWarning: coroutine 'processing_docs' was never awaited
processing_docs(base_link + titles.attrib['href'])
requests
无论如何都会同步执行HTTP查询。你需要通过loop.run_in_executor()
来运行requests代码或者替换成aiohttp。 - Andrew Svetlovrequests
是一个同步库。 你可以通过在requests.get()
调用之前缺少await
来确定它。 - Andrew Svetlov