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

单章小说下载:

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)

结果展现:

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

相关推荐
二十雨辰17 小时前
[爬虫]-request
爬虫
狗都不学爬虫_1 天前
AI逆向 - 99aq中心滑块验证+登录(wasm纯算)
爬虫·python·网络爬虫
hyf3266332 天前
泛程序:从零开始搭建稳定程序项目框架
运维·服务器·爬虫·百度·seo
进击的雷神2 天前
攻克俄语编码适配与无ID字段去重:基于纯URL拼接的国际化爬虫设计
爬虫·spiderflow
tang777892 天前
爬虫频繁429封禁?代理IP轮换+请求指纹优化全流程解决方案
爬虫·python·网络协议·tcp/ip·动态代理ip·免费代理ip
JackSparrow4142 天前
前端安全之JS混淆+请求加密+请求签名以提升爬虫难度
前端·javascript·后端·爬虫·python·安全
kisloy3 天前
【爬虫入门第5讲】Python爬虫文本解析详解
开发语言·爬虫·python
上海云盾-小余3 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
喜欢吃豆3 天前
Playwright 深度解析:从浏览器自动化到动态爬虫、自动化测试与 AI Agent
运维·爬虫·自动化
kisloy3 天前
【爬虫入门第10讲】Scrapy 框架深度解析(一):五大组件与中间件
爬虫·scrapy·中间件