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

单章小说下载:

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)

结果展现:

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

相关推荐
小花皮猪2 小时前
Bright Data CLI 入门教程:命令行实现 Web Scraping 与结构化数据采集
爬虫
Maiko Star4 小时前
Python项目实战——网络机器人(爬虫)
爬虫·python·机器人
STDD6 小时前
n8n 实战工作流模板:GitHub→飞书通知、定时爬虫→数据库、Webhook→多渠道推送
爬虫·github·飞书
跨境数据猎手21 小时前
反向海淘实战|独立站搭建+国内电商API对接
开发语言·爬虫·架构
电商API_180079052471 天前
电商ERP 自动同步订单功能实现拆解与技术手段
服务器·前端·网络·爬虫
绘梨衣5471 天前
求助帖:pdf复杂表格解析
爬虫·python·pdf
huangdong_2 天前
电商图片下载工具技术路线全面解析:从爬虫方案到浏览器方案的完整技术演进与选型深度指南
爬虫
江华森3 天前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python
狗都不学爬虫_3 天前
AI逆向 - 某定制瑞树6纯算(ck-header-params后缀)
爬虫·python·网络爬虫
亿牛云爬虫专家3 天前
2026架构前沿:将Declarative Crawler(声明式爬虫)引入你的技术栈
爬虫·架构·爬虫代理·requests·隧道代理·声明式爬虫·转发模式