python的异步请求

python的异步请求,python,asynchronous,aiohttp,Python,Asynchronous,Aiohttp,我尝试使用aiohttp和asyncio来执行请求,但是我得到了错误 '需要asyncio.Future、协程或可等待的' 这是我的代码。我如何修复它 import requests from bs4 import BeautifulSoup import asyncio import aiohttp res = requests.get('https://www.rottentomatoes.com/top/') soup = BeautifulSoup(res.text,'lxml') m

我尝试使用aiohttp和asyncio来执行请求,但是我得到了错误

'需要asyncio.Future、协程或可等待的'

这是我的代码。我如何修复它

import requests
from bs4 import BeautifulSoup
import asyncio
import aiohttp

res = requests.get('https://www.rottentomatoes.com/top/')
soup = BeautifulSoup(res.text,'lxml')
movie_list=[]
for link in soup.select('section li a[href]'):
    movie_list.append('https://www.rottentomatoes.com'+link.get('href'))

async def request(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            body = await resp.text(encoding='utf-8')
            soup =BeautifulSoup(body,'lxml')
            movie = []
            async for link in soup.select('tbody tr td a '):
                await movie.append(link.get('href'))

        return  movie

async def main():
    results = await asyncio.gather(*[request(url) for url in movie_list])
    print(results)
    return results
print(movie_list)

loop = asyncio.get_event_loop()
results = loop.run_until_complete(main)

您需要调用
loop.run_直到_完成(main())
,而不仅仅是函数
main
(不带括号)。接下来,您不需要
soup.select()中的
async
关键字。我还更改了一个select字符串来解析某些内容:

import requests
from bs4 import BeautifulSoup
import asyncio
import aiohttp

res = requests.get('https://www.rottentomatoes.com/top/')
soup = BeautifulSoup(res.text,'lxml')
movie_list=[]

for link in soup.select('section li a[href]'):
    movie_list.append('https://www.rottentomatoes.com'+link.get('href'))

async def request(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            body = await resp.text(encoding='utf-8')
            soup = BeautifulSoup(body,'lxml')
            movie = []
            # no need to call async for here!
            for link in soup.select('section#top_movies_main table a'):
                movie.append(link['href'])

        return  movie

async def main():
    results = await asyncio.gather(*[request(url) for url in movie_list])
    print(results)
    return results

print(movie_list)

loop = asyncio.get_event_loop()
results = loop.run_until_complete(main())   # you need to create coroutine
印刷品:

['https://www.rottentomatoes.com/top/bestofrt/top_100_action__adventure_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_animation_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_art_house__international_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_classics_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_comedy_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_documentary_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_drama_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_horror_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_kids__family_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_musical__performing_arts_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_mystery__suspense_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_romance_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_science_fiction__fantasy_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_special_interest_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_sports__fitness_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_television_movies/', 'https://www.rottentomatoes.com/top/bestofrt/top_100_western_movies/']
[['/m/mad_max_fury_road', '/m/1013775-metropolis', '/m/wonder_woman_2017', '/m/logan_2017', '/m/1011615-king_kong', '/m/zootopia', '/m/1000355-adventures_of_robin_hood', '/m/star_wars_episode_vii_the_force_awakens', 

... and so on

您过度使用了异步。。。这不是一个神奇的词。最好先阅读有关asyncio的教程。扩展到MatrixTai的评论:仅仅因为你在一个协同程序中,并不意味着每个for循环都需要一个
async
,每个调用都需要
wait
ed。在
汤中循环时删除
async
。从
电影中选择
wait
。在
请求
中追加
,因为它们不是异步的。谢谢你的帮助。我删除了异步并等待。但是仍然得到了错误“一个异步IO。未来,一个协程或一个等待是必需的”请发布完整的异常,并进行回溯,而不仅仅是对它的描述。即使这对你来说毫无意义,它也会告诉那些试图帮助你的人问题的真正所在。