import asyncio
import time
import aiohttp
from lxml import etree
urls = [
'https://www.baidu.com/',
'https://www.baidu.com/',
'https://www.baidu.com/'
]
async def get_request(url):
async with aiohttp.ClientSession() as sess:
async with await sess.get(url=url) as response:
await asyncio.sleep(2)
page_text = await response.text()
return page_text
def parse(t):
page_text = t.result()
tree = etree.HTML(page_text)
parse = tree.xpath('//*[@id="s_xmancard_news_new"]/div/div[1]/ul/li[1]/a/span[2]')
print(parse)
if __name__ == '__main__':
start = time.time()
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))
print('总好事:', time.time()-start)