Python爬虫康复训练——笔趣阁《神魂至尊》

还是话不多说,很久没写爬虫了,来个bs4康复训练爬虫,正好我最近在看《神魂至尊》,爬个txt文件下来看看

直接上代码

python 复制代码
"""
神魂至尊网址-https://www.bqgui.cc/book/1519/
"""
import requests
from bs4 import BeautifulSoup
import os

A=[]#存储章节标题
B=[]#存储章节链接
url='https://www.bqgui.cc/book/1519/'
header={
    'Referer':'https://www.bqgui.cc/s?q=%E7%A5%9E%E9%AD%82%E8%87%B3%E5%B0%8A',
    'Cookie':'Hm_lvt_52624d0257fe48ed9dea61ff01fa3417=1720163550; HMACCOUNT=79B595C42B32BA19; hm=9a7ca0f0fe759c15c8c93eed6eb59f86; Hm_lpvt_52624d0257fe48ed9dea61ff01fa3417=1720163795',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
}
reponse = requests.get(url,headers=header)
#print(reponse.text)
html = BeautifulSoup(reponse.text,'lxml')
htmls = html.select('div.listmain dl dd a')
for a in htmls:
    # 检查a的文本内容中是否包含特定字符串
    if '<<---展开全部章节--->>' not in a.text:
        # 如果不包含,则添加到列表A和B中
        A.append(a.text)
        B.append('https://www.bqgui.cc/' + a['href'])
for j in B:
    urls =j
    headers={
        'Cookie':'Hm_lvt_52624d0257fe48ed9dea61ff01fa3417=1720163550; HMACCOUNT=79B595C42B32BA19; hm=9a7ca0f0fe759c15c8c93eed6eb59f86; Hm_lpvt_52624d0257fe48ed9dea61ff01fa3417=1720164854',
        'Referer':'https://www.bqgui.cc/book/1519/',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
    }
    reponses = requests.get(urls,headers=headers)
    lxml = BeautifulSoup(reponses.text,'lxml')
    lxmls = lxml.select('div.Readarea.ReadAjax_content')
    for k in lxmls:
        #print(k.text)
        for l in A:
            directory = '神魂至尊'
            if not os.path.exists(directory):
                os.makedirs(directory)
            with open(f'{'神魂至尊'}/{l}','a')as f:
                f.write(k.text + '\n')

效果图

相关推荐
呆呆的小草2 小时前
Cesium距离测量、角度测量、面积测量
开发语言·前端·javascript
uyeonashi2 小时前
【QT系统相关】QT文件
开发语言·c++·qt·学习
鹏码纵横3 小时前
已解决:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 异常的正确解决方法,亲测有效!!!
java·python·mysql
仙人掌_lz3 小时前
Qwen-3 微调实战:用 Python 和 Unsloth 打造专属 AI 模型
人工智能·python·ai·lora·llm·微调·qwen3
冬天vs不冷3 小时前
Java分层开发必知:PO、BO、DTO、VO、POJO概念详解
java·开发语言
sunny-ll3 小时前
【C++】详解vector二维数组的全部操作(超细图例解析!!!)
c语言·开发语言·c++·算法·面试
猎人everest4 小时前
快速搭建运行Django第一个应用—投票
后端·python·django
猎人everest4 小时前
Django的HelloWorld程序
开发语言·python·django
嵌入式@秋刀鱼4 小时前
《第四章-筋骨淬炼》 C++修炼生涯笔记(基础篇)数组与函数
开发语言·数据结构·c++·笔记·算法·链表·visual studio code
嵌入式@秋刀鱼4 小时前
《第五章-心法进阶》 C++修炼生涯笔记(基础篇)指针与结构体⭐⭐⭐⭐⭐
c语言·开发语言·数据结构·c++·笔记·算法·visual studio code