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

单章小说下载:

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)

结果展现:

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

相关推荐
ZC跨境爬虫14 小时前
纯requests+Redis实现分布式爬虫(可视化4终端,模拟4台电脑联合爬取)
redis·分布式·爬虫·python
以神为界1 天前
Python入门实操:基础语法+爬虫入门+模块使用全指南
开发语言·网络·爬虫·python·安全·web
ZC跨境爬虫1 天前
Scrapy实战:5sing原创音乐网多页数据爬取(完整可运行,附避坑指南)
爬虫·python·scrapy·html
llm大模型算法工程师weng1 天前
Python爬虫实现指南:从入门到实战
开发语言·爬虫·python
一点 内容1 天前
Scrapy框架深度解析:高效构建分布式爬虫的实战指南
分布式·爬虫·scrapy
kisloy1 天前
【反爬虫】极验4 W参数逆向分析
java·javascript·爬虫
wanhengidc1 天前
服务器如何防范爬虫攻击?
运维·服务器·网络·爬虫·游戏·智能手机
小白学大数据2 天前
Python 爬虫:拍卖网站列表页与详情页数据联动爬取
开发语言·爬虫·python
HP-Patience2 天前
【爬虫脚本自动化录制】playwright codegen使用教程
运维·爬虫·自动化
深蓝电商API2 天前
淘宝商品详情页逆向:无需API获取SKU价格与库存的爬虫方案
爬虫·api·反向海淘