import asyncio
from asyncio import tasks
import aiohttp #pip install aiohttp
from lxml import etree
for x in range(498232,498242):
async def fetch(session,url):
async with session.get(url) as response:
html=await response.text()
return html
async def parser_content(html):
selector = etree.HTML(html)
title=str(selector.xpath("//div[@class='read_title']//h1[1]/text()")[0])
print(x,title)#怎么x固定不变?
async def download_content(url):
async with aiohttp.ClientSession() as session:
html=await fetch(session,url)
await parser_content(html)
tasks=[
asyncio.ensure_future(download_content('https://www.某网站om/34953/{}.html'.format(x)))
]
loop=asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks))
运行结果如下: