吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1892|回复: 7
收起左侧

[Python 转载] python协程爬取笔趣阁小说

[复制链接]
ixhao 发表于 2022-8-15 22:35

最近在学习python爬虫的协程部分,所以拿一个小说网站来练手。
首先使用同步爬取抓取小说章节所有的url,然后用异步的方法来保存小说内容。以txt文件保存。

食用方法:

python环境为 python 3.9
用到的库有requests,BeautifulSoup,asyncio,aiohttp,aiofiles,使用源码要先安装库

遇到的问题:

小说一共有1400+章节,但是每次爬取都只能爬1200+左右,估计是timeout错误或者400,但是400的话我用浏览器能打开,不知道怎么解决,希望有大哥你能够帮忙解决一下。
vdzha4.png

报错异常

Task exception was never retrieved
future: <Task finished name='第1492章那座洞府那壶桃夭酿' coro=<get_content() done, defined at E:\pachong\bqg_yuanzun.py:8> exception=ClientPayloadError("400, message='Can not decode content-encoding: gzip'")>
Traceback (most recent call last):
  File "E:\pachong\bqg_yuanzun.py", line 11, in get_content
    response = await resp.content.read()
  File "D:\python\lib\site-packages\aiohttp\streams.py", line 349, in read
    raise self._exception
aiohttp.client_exceptions.ClientPayloadError: 400, message='Can not decode content-encoding: gzip'

代码贴出

import requests
from bs4 import BeautifulSoup
import asyncio
import aiohttp
import aiofiles

async def get_content(xs_url, name):
    async with aiohttp.ClientSession() as session:
        async with session.get(xs_url) as resp:
            response = await resp.content.read()
            soup = BeautifulSoup(response, "html.parser")
            content = soup.find("div", id="content")
            data = content.text.split()
            data = "\r\n".join(data)
            async with aiofiles.open(f"novel/{name}.txt",mode="a",encoding="utf-8") as f:
                await f.write(data)
            print(f"已经下载{name}")

async def main(url):
    resp = requests.get(url)

    soup = BeautifulSoup(resp.text, "html.parser").find("div", attrs={"id": "list"}).find_all("dd")[9:]

    tasks = []
    print("开始创建异步任务")
    for i in soup:
        xs_url = url + i.find("a").get("href").lstrip("/258").lstrip("02/")
        name = i.find("a").text
        tasks.append(asyncio.create_task(get_content(xs_url, name),name=name))
        # print(url + xs_url,name)
    await asyncio.wait(tasks)
    print("结束异步任务")

if __name__ == '__main__':
    print("开始执行")

    main_url = "http://www.biqugse.com/25802/"

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(main_url))

vdz4IJ.png

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

外酥内嫩 发表于 2022-8-16 11:44
我感觉你在看他的爬虫视频,因为我也看过
MyModHeaven 发表于 2022-8-16 13:02
看视频学的吗?如果是在线,方便的话,给个链接,谢谢啦
 楼主| ixhao 发表于 2022-8-16 20:53
MyModHeaven 发表于 2022-8-16 13:02
看视频学的吗?如果是在线,方便的话,给个链接,谢谢啦

是B站的武沛齐老师,可以去搜一下
MyModHeaven 发表于 2022-8-17 10:09
ixhao 发表于 2022-8-16 20:53
是B站的武沛齐老师,可以去搜一下

多谢了我去看看
shiwowo 发表于 2022-8-23 22:55
提示缺少什么api 不懂 哇
 楼主| ixhao 发表于 2022-8-25 16:21
shiwowo 发表于 2022-8-23 22:55
提示缺少什么api 不懂 哇

没有安装的库需要安装
virsnow 发表于 2022-10-4 12:19
体验一下谢谢~
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 07:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表