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

单章小说下载:

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)

结果展现:

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

相关推荐
隔窗听雨眠1 小时前
大模型加爬虫上篇:技术融合与架构革新
爬虫·架构
Super Scraper5 小时前
如何批量抓取 TikTok 数据而不被封锁?完整指南
爬虫·ai·自动化·抖音·tiktok·ai agent
深蓝电商API6 小时前
自动化录屏 + 截图:打造爬虫调试的上帝视角
爬虫
tang777897 小时前
市场调研自动化采集架构:基于住宅IP轮换的APP数据抓取与反风控方案
爬虫·动态代理ip·爬虫代理ip·爬虫动态ip·住宅代理ip·动态住宅ip
数据知道7 小时前
指纹浏览器环境的导入、导出、快照与云端同步机制
爬虫·数据采集·指纹浏览器
星川皆无恙7 小时前
大数据k-means聚类算法:基于k-means聚类算法+NLP微博舆情数据爬虫可视化分析推荐系统(新版)
大数据·人工智能·爬虫·算法·机器学习·自然语言处理·kmeans
小二·9 小时前
Rust 爬虫与数据处理实战:大规模并发抓取 + 流式处理
开发语言·爬虫·rust
在放️19 小时前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python
隔窗听雨眠20 小时前
大模型加爬虫中篇:工程实践与应用场景
爬虫