利用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)
相关推荐
helloweilei11 小时前
python 抽象基类
python
用户83562907805112 小时前
Python 实现 PPT 转 HTML
后端·python
zone773917 小时前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone773917 小时前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒1 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习1 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽2 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly2 天前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
vivo互联网技术2 天前
ICLR2026 | 视频虚化新突破!Any-to-Bokeh 一键生成电影感连贯效果
人工智能·python·深度学习