Python-爬取小说内容并下载

# 文章首页链接
url = "https://www.17k.com/chapter/108821/3148523.html"
def book_spider():
    # 爬取并下载小说内容
    import requests
    from bs4 import BeautifulSoup
    import time
    headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}
    while True:
        global url
        resp = requests.get(url, headers=headers).content
        time.sleep(1)
        html = BeautifulSoup(str(resp, "utf-8"), "lxml")
        # 获取下一章链接
        a_tag = html.find_all("a", class_="nextChapter")
        if len(a_tag) != 2:
            break
        next_tag = a_tag[0]["href"]
        url = "https://www.17k.com" + next_tag
        for tag in html.find_all("div", class_="readAreaBox content"):
            title = tag.find_all("h1")
            # txt文件末尾追加
            with open('小说.txt', 'a+', encoding="utf-8") as f:
                f.write(title[0].string + "\n")  # 标题写入文件
            content = tag.find_all("p")
            for i in content:
                if len(i) == 0 or len(i) == 3:
                    continue
                else:
                    if len(i.string) > 90:
                        with open('小说.txt', "a+", encoding="utf-8") as f:
                            # 分行写入数据,每行最多90个字符串
                            f.writelines("\t%s\n%s" % (i.string[:90], i.string[90:]))
                    else:
                        with open('小说.txt', "a+", encoding="utf-8") as f:
                            f.writelines("\t%s\n" % (i.string))

book_spider()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Enougme

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值