import aiofiles,aiohttp,asyncio,time
from lxml import etree
async def get(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
res=await resp.text()
return res
async def xr(url,aa,dz):
res=await get(url)
ress=etree.HTML(res)
a=ress.xpath('//*[@id="content"]/text()')
for i in a:
async with aiofiles.open(rf'{dz}\{aa}.txt','a',encoding='utf-8') as f:
await f.write(i.strip())
print(f'第{aa}章下好啦')
async def zy(pid):
dz=input('请输入您要保存的地址:')
aa=0
taks=[]
new_url=f'https://www.biqugeu.net{pid}'
res=await get(new_url)
ress=etree.HTML(res)
a=ress.xpath('//*[@id="list"]/dl//a/@href')
for i in range(12,len(a)):
aa+=1
urll=f'https://www.biqugeu.net{a}'
taks.append(asyncio.create_task(xr(urll,aa,dz)))
await asyncio.wait(taks)
async def main(name):
res=await get(f'https://www.biqugeu.net/searchbook.php?keyword={name}')
ress=etree.HTML(res)
a=ress.xpath('//*[@id="hotcontent"]/div/div/dl/dt/a/@href')
await zy(a)
if __name__=='__main__':
name=input('请输入您要下载的书名:')
time1=time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(main(name))
time2=time.time()
print(f'下载完成啦,一共用了{time2-time1}s')
input('Press Enter to exit…') 不会用,支持下 高手,学习了 百千三昧 发表于 2021-4-28 17:44
你弄出来咋这么清晰,我复制就跟一坨屎一样
选插入代码,你这个是IDE上直接复制下来粘上去的 学习协程的写法 听说高并发IO任务推荐用多线程
高CPU任务推荐用协程
不知道是不是这样 最近笔仙阁的服务器压力很大啊
页:
[1]
2