在尝试爬取一本小说时报错,下面附完整代码和报错的问题
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<download() done, defined at C:\Users\lenovo\Desktop\test\水浒传\水浒传模仿.py:7> exception=AttributeError('__aenter__')>
Traceback (most recent call last):
File "C:\Users\lenovo\Desktop\test\水浒传\水浒传模仿.py", line 8, in download
async with aiohttp.ClientSession as session: #相当于request
AttributeError: __aenter__
import asyncio
import aiohttp
import re
import requests
import aiofiles
async def download(http,name):
async with aiohttp.ClientSession as session: #相当于request
async with session.get(http) as resp2: #相当于resp
way2 = re.compile('r<div class="xstext">(?P<text>.*?)<div class="pagelist" id="pages">,re.S')
href2 = way2.finditer(resp2.text)
for it in href2:
async with aiofiles.open(name,mode = "wb") as f:
await f.write(it.group("text"))
async def web(url1):
tasks = []
url = "https://www.gdwxcn.com/gdxs/shz/"
resp = requests.get(url)
resp.encoding = "gbk"
way = re.compile(r'<li><a href="/gdxs/shz/(?P<web>.*?)" target="_blank" >(?P<name>.*?)</a></li>', re.S)
href = way.finditer(resp.text)
for i in href:
http = "https://www.gdwxcn.com/gdxs/shz/" + i.group("web")
name = i.group("name")
tasks.append(asyncio.create_task(download(http,name))) #将任务添加到下载函数download中
await asyncio.wait(tasks)
if __name__ == '__main__':
url ="https://www.gdwxcn.com/gdxs/shz/"
asyncio.run(web(url))
我觉得你应该测试一下,正则匹配的字符串网址那,部分是否问题
as session 前面那个应该加括号,没记错的话
在write部分做了关于字节和文本的问题做修改了,但是仍然不能跑,
问题解决了吗?我也是这个问题