利用python抓取小说,爬虫抓取小说

1.https://www.bqg70.com/ 首先进入这个网址,进入笔趣阁官网

2.搜索你想要看的小说

3.选择你想看的小说后,在地址栏会出现一个数字,举例:"https://www.bqg70.com/book/3315/"

那个数字请复制好,例如:"3315"

4.运行代码,输入这个数字 ,即可下载对应的小说

5.安装包

pip install requests

pip install parsel

pip install prettytable

代码如下:

python 复制代码
import requests  # 第三方的模块
import parsel  # 第三方的模块
import os  # 内置模块 文件或文件夹

filename = '小说\\'
if not os.path.exists(filename):
    os.mkdir(filename)

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}

    

rid = input('输入书名ID:')
link = f'https://www.bqg70.com/book/{rid}/'

html_data = requests.get(url=link, headers=headers).text
# print(html_data)
selector_2 = parsel.Selector(html_data)
divs = selector_2.css('.listmain dd')
for div in divs:
    title = div.css('a::text').get()
    href = div.css('a::attr(href)').get()
    url = 'https://www.bqg70.com' + href

    try:
        response = requests.get(url=url, headers=headers)
        selector = parsel.Selector(response.text)
        # getall 返回的是一个列表 []
        book = selector.css('#chaptercontent::text').getall()
        book = '\n'.join(book)
        # 数据保存
        with open(filename + title + '.txt', mode='a', encoding='utf-8') as f:
            f.write(book)
            print('正在下载章节:  ', title)
    except Exception as e:
        print(e)
相关推荐
阿猿收手吧!40 分钟前
【C++】Ranges:彻底改变STL编程方式
开发语言·c++
云游云记1 小时前
php 随机红包数生成
开发语言·php·随机红包
程序员林北北1 小时前
【前端进阶之旅】JavaScript 一些常用的简写技巧
开发语言·前端·javascript
feasibility.1 小时前
yolo11-seg在ISIC2016医疗数据集训练预测流程(含AOP调loss函数方法)
人工智能·python·yolo·计算机视觉·健康医疗·实例分割·isic2016
gAlAxy...2 小时前
MyBatis-Plus 核心 CRUD 操作全解析:BaseMapper 与通用 Service 实战
java·开发语言·mybatis
开开心心就好2 小时前
一键加密隐藏视频,专属格式播放工具
java·linux·开发语言·网络·人工智能·macos
L念安dd2 小时前
基于 PyTorch 的轻量推荐系统框架
人工智能·pytorch·python
CUC-MenG2 小时前
Codeforces Round 1079 (Div. 2)A,B,C,D,E1,E2,F个人题解
c语言·开发语言·数学·算法
阿里嘎多学长2 小时前
2026-02-07 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Liue612312312 小时前
YOLO11改进策略卷积篇使用C3k2-PPA替换YOLO11中的卷积即插即用简单高效
python