代码如下:
import asyncio
import aiohttp
from lxml import etree
urls = [
'http://127.0.0.1:5000/bobo',
'http://127.0.0.1:5000/jay',
'http://127.0.0.1:5000/tom',
]
async def get_request(url):
async with aiohttp.ClientSession() as sess:
async with await sess.get(url=url) as response:
page_text = await response.text()
return page_text
def parse(t):
page_text = t.result()
et = etree.HTML(page_text)
a = et.xpath('//a[@id="feng"]/@href')[0]
print(a)
if __name__ == '__main__':
tasks = []
for url in urls:
c = get_request(url)
task = asyncio.ensure_future(c)
task.add_done_callback(parse)
tasks.append(task)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
DeprecationWarning: There is no current event loop task = asyncio.ensure_future(c)
DeprecationWarning: There is no current event loop loop = asyncio.get_event_loop()
将下面一句:
loop = asyncio.get_event_loop()
替换成下面两句即可
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
依然不行
请各位能人志士帮个忙