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

单章小说下载:

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)

结果展现:

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

相关推荐
程序员威哥3 小时前
实战!Python爬京东商品评论:从采集到情感分析+词云可视化,新手30分钟跑通
开发语言·爬虫·python·scrapy
S1998_1997111609•X13 小时前
哈希树函数洪水泛滥污染孪生镜像导致生物量子信息泄露以钩子而爬虫植入ssd探测
爬虫·网络协议·缓存·哈希算法·开闭原则
捉鸭子14 小时前
QQ音乐sign vmp逆向
爬虫·python·网络安全·网络爬虫
上海云盾王帅16 小时前
如何防御爬虫攻击:告别数据被扒,构建智能业务风控体系
爬虫
不会飞的鲨鱼17 小时前
观鸟网 RSA加密 AES 解密
javascript·爬虫·python
pengyi87101518 小时前
HTTP与HTTPS代理基础区别,协议原理通俗解析
网络·爬虫·网络协议·tcp/ip·智能路由器
科技牛牛1 天前
AI爬虫引爆代理IP产业:一场正在发生的数据粮草争夺战
人工智能·爬虫·tcp/ip·数据安全·ip地址查询
小白学大数据1 天前
JS 混淆加密下的 Python 爬虫解决方案
javascript·爬虫·python
艺杯羹1 天前
Tkinter实战:为CSDN爬虫打造可视化界面,从GUI到多线程完整方案
爬虫·python·开源