Python爬虫实战(基础篇)—5获取xx小说(附完整代码)

转眼将就来到了我们爬虫基础课的第5 节课,今天我们来获取小说网 网站的一些内容来进行阅读学习

PS前面几节课的内容在专栏这里,欢迎大家考古:点我

在这里插入图片描述

首先我们看一下,今天我们来随机获取一本书的内容

在这里插入图片描述

首先第 1 步,我们要点击第一章,查看URL

我们可以看到:最后的【57628310.html】这就是每一章节的页码数,只要找到虽有的页码数,那么就可以获取其内容

在这里插入图片描述

接下来我们获取其第一章的内容试一下

代码1(获取网页内容-完整版在最后):

import requests
url = 'https://book.zongheng.com/chapter/867252/57628310.html'
book_data = requests.get(url=url).text
print(book_data)

OK ,成功拿到!

在这里插入图片描述

第 2 步:数据清洗

代码2(数据清洗-完整版在最后)

import re
import requests
url = 'https://book.zongheng.com/chapter/867252/57628310.html'
res_str = requests.get(url=url).text
# print(res_str)
title = re.findall(r'name="keywords" content="(.*?)"/>',res_str)[0]
book_content= re.findall(r'<p>(.*?)</p>',res_str)
# print(book_content)
print(title)
for c in book_content[:-1]:
    print(c)

在这里插入图片描述

第三步,获取所有章节:

点击【目录】,查看章节来源的URL

由此发现,每一本书都有自己的 id,参数为:【_】下划线

在这里插入图片描述

代码 3 (获取所有章节的页码数+数据清洗——完整版在最后)

book_id = '1690437685919'
data = requests.get("https://book.zongheng.com/showchapterList/867252?_={book_id}").text
data = json.loads(data)
# print(data)
for i in data['data']['showTomeViewList'][0]['chapterViewList']:
    print(f'章节:{i["chapterName"]},章节id:{i["chapterId"]}')

在这里插入图片描述

与代码2进行合并,形成完整版代码:

在这里插入图片描述

完整版代码:

book_id = ‘1690437685919’,这里可以替换自己喜欢的bookid

import json
import requests
book_id = '1690437685919'
data = requests.get("https://book.zongheng.com/showchapterList/867252?_={book_id}").text
data = json.loads(data)
# print(data)
for i in data['data']['showTomeViewList'][0]['chapterViewList']:
    with open(f"./结果/xxxx-{i['chapterName']}",'w',encoding='utf-8') as f:
        print(f'章节:{i["chapterName"]},章节id:{i["chapterId"]}')

        url = f'https://book.zongheng.com/chapter/867252/{i["chapterId"]}.html'
        res_str = requests.get(url=url).text
        # print(res_str)
        title = re.findall(r'name="keywords" content="(.*?)"/>',res_str)[0]
        book_content= re.findall(r'<p>(.*?)</p>',res_str)
        # print(book_content)
        print(title)
        for c in book_content[:-1]:
            # print(c)
            f.write(c+'\n')

希望对大家有帮助

我最近在努力学习爬虫,大家共同进步!!

都看到这了,关注+点赞+收藏=不迷路!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一晌小贪欢

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

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

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

打赏作者

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

抵扣说明:

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

余额充值