爬虫小案例爬取塔某小说内容

单章小说下载:

python 复制代码
import requests
import parsel
import re
#https://www.tadu.com/getPartContentByCodeTable/1004090/2 第二章链接
def get_response(url):
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    response = requests.get(url=url,headers=headers)

    return response
if __name__ == '__main__':
    url = 'https://www.tadu.com/getPartContentByCodeTable/1004090/1'
    response = get_response(url).json()
    content = response['data']['content']
    result = re.findall('<p.*?>(.*?)</p>',content)
    string = '\n'.join(result)
    print(string)
    with open('你那近三十的心酸.txt','w',encoding='utf-8') as f:
        f.write(string)

结果展现:

整本小说下载,下载第二章只要在url最后改成2即可

实现搜索界面展现:

python 复制代码
def get_search(url,key):
    data = {
        'query': key,
    }
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    response = requests.post(url=url,data=data,headers=headers)
    return response

def get_booklist(response):
    selector = parsel.Selector(text=response)
    ul_list = selector.css('.bookList li')
    for ul in ul_list:
        href = ul.css('div.rtList a::attr(href)').get()
        title = ul.css('div.rtList a.bookNm::text').getall()
        title = ''.join(title)
        info = ul.css('div.rtList a.bookIntro::text').getall()
        print(href,title,info)
if __name__ == '__main__':
    url = 'https://www.tadu.com/search'
    key = '仙魔'
    resp = get_search(url,key).text
    # print(resp)
    get_booklist(resp)

结果展现:

可以通过搜索姐界面找到小说名称和小说链接,小说简介。

相关推荐
深蓝电商API20 小时前
LLM正在改变爬虫:Crawl4AI源码深度解析
爬虫·crawl4ai
Chengbei113 天前
JsOwl|AI 赋能 JS 敏感信息扫描器,AST + 正则、source‑map 源码还原、登录态爬虫输出交互式 HTML 报告
开发语言·javascript·网络·人工智能·爬虫·网络安全·自动化
远航计算机3 天前
AI 答案里的链接,为什么开始分等级了?
大数据·人工智能·爬虫·aigc
Novproxy_Z3 天前
爬虫任务被拦截:区分 IP 封禁、设备封禁、账号封禁
爬虫·网络协议·tcp/ip
郝学胜-神的一滴3 天前
Python浏览器自动化:Selenium与Playwright实战对比,网页爬虫&自动操作一站式指南
开发语言·爬虫·python·selenium·程序人生·自动化
Python大数据分析@3 天前
通过 MCP 实现 AI Agent自动化数据采集
运维·人工智能·爬虫·自动化
深蓝电商API3 天前
Docker+K8s部署爬虫:从单机到集群的完整路线
爬虫·docker·k8s
深蓝电商API4 天前
亿级数据的存储选型:MongoDB/ES/ClickHouse横评
爬虫·clickhouse·mongodb·elasticsearch
重生之高冷汉堡包4 天前
YouTube 数据抓取出现 429/403 错误?2026 反爬机制拆解与爬虫选型指南
爬虫
钱栈up4 天前
扫完 3.5 万文件后,我发现了 521 组散落的同名文件
爬虫·python·数据分析