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

单章小说下载:

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)

结果展现:

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

相关推荐
深蓝电商API9 小时前
如何快速定位加密函数?
爬虫·加密函数
oh,huoyuyan20 小时前
网页公开数据采集工具推荐
人工智能·爬虫·火车采集器
崔子末21 小时前
某影视库剧集列表以及查询接口逆向
爬虫·python
雪山青木1 天前
全国微博签到数据201912-202004
大数据·爬虫·python·数据挖掘·数据分析·新浪微博
TlSfoward1 天前
DLL 指纹库 采集、检索 TLSFOWARD tls指纹库
爬虫·网络协议·https·自动化
有味道的男人2 天前
基于 Codex 爬虫构建亚马逊无人上架系统|竞品详情采集到商品发布完整实现
爬虫·亚马逊
张小凡vip2 天前
Python爬虫实战:高效稳定的文件下载模块设计与实现
开发语言·爬虫·python
tang777893 天前
爬虫代理报错速查:407/408/409/410/503 从报错到根治(实测可用)
爬虫·爬虫代理·动态代理ip·代理ip·503·407·爬虫报错
小白学大数据3 天前
Python 爬虫实战:抓取汽车之家二手车成交价格与里程数据
开发语言·爬虫·python·汽车
张小凡vip4 天前
python--爬虫--成熟的爬虫框架Scrapy
爬虫·python·scrapy