use*_*046 9 python asynchronous python-3.x python-asyncio aiohttp
I'm trying to make an asynchronous web scraper using beautifulsoup and aiohttp.This is my initial code to start things.I'm getting a [TypeError: An asyncio.Future, a coroutine or an awaitable is required] and having a hard time figuring out what is wrong with my code.I am new to python and would appreciate any help regarding this.
import bs4
import asyncio
import aiohttp
async def parse(page):
soup=bs4.BeautifulSoup(page,'html.parser')
soup.prettify()
print(soup.title)
async def request():
async with aiohttp.ClientSession() as session:
async with session.get("https://google.com") as resp:
await parse(resp)
loop=asyncio.get_event_loop()
loop.run_until_complete(request)
Run Code Online (Sandbox Code Playgroud)
Traceback:-
Traceback (most recent call last):
File "C:\Users\User\Desktop\Bot\aio-req\parser.py", line 21, in <module>
loop.run_until_complete(request)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 591, in run_until_complete
future = tasks.ensure_future(future, loop=self)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\tasks.py", line 673, in ensure_future
raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required
Run Code Online (Sandbox Code Playgroud)
wwi*_*wii 13
一个问题loop.run_until_complete(request)应该是loop.run_until_complete(request())- 您实际上必须调用它才能返回协程。
还有其他问题 - 就像您将aiohttp.ClientResponse对象传递给parse并将其视为 text/html 一样。我让它与以下一起工作,但不知道它是否适合您的需求,因为 parse它不再是一个协程。
def parse(page):
soup=bs4.BeautifulSoup(page,'html.parser')
soup.prettify()
return soup.title
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def request():
async with aiohttp.ClientSession() as session:
html = await fetch(session, "https://google.com")
print(parse(html))
if __name__ == '__main__':
loop=asyncio.get_event_loop()
loop.run_until_complete(request())
Run Code Online (Sandbox Code Playgroud)
这也有效:
def parse(page):
soup=bs4.BeautifulSoup(page,'html.parser')
soup.prettify()
print(soup.title)
async def request():
async with aiohttp.ClientSession() as session:
async with session.get("https://google.com") as resp:
parse(await resp.text())
Run Code Online (Sandbox Code Playgroud)
最后,您的原始代码,将一个可等待的响应对象传递给parse然后awaiting for page.text()。
async def parse(page):
soup=bs4.BeautifulSoup(await page.text(),'html.parser')
soup.prettify()
print(soup.title)
async def request():
async with aiohttp.ClientSession() as session:
async with session.get("https://google.com") as resp:
await parse(resp)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13552 次 |
| 最近记录: |