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

单章小说下载:

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跨境爬虫6 小时前
免费验证码识别:用ddddocr实现Playwright自动化登录
爬虫·python·自动化
ZC跨境爬虫6 小时前
Playwright进阶操作:鼠标拖拽与各类点击实战(含自定义拖拽实例)
前端·爬虫·python·ui
Pocker_Spades_A7 小时前
Python快速入门专业版(五十六)——爬虫会话管理:Cookie与Session原理及实战(保持登录状态)
开发语言·爬虫·python
进击的雷神1 天前
攻克多级导航循环与class属性ID提取:基于双层循环架构的精准爬虫设计
爬虫·架构
ZC跨境爬虫1 天前
Playwright核心操作实战精讲(QQ空间+百度+iframe,含等待_键盘_iframe操作)
前端·爬虫·python·计算机外设
ZC跨境爬虫1 天前
对称加密算法详解(DES篇):特点、实现与逆向实操
前端·javascript·爬虫
南通DXZ1 天前
Python爬虫进阶:Selenium模拟真人操作(解决反爬+动态网页)
爬虫·python·selenium
shark22222222 天前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
Blurpath住宅代理2 天前
社交媒体数据采集技术指南:从合规获取到营销洞察
网络·爬虫·动态代理·数据抓取·住宅ip·住宅代理
JoshRen2 天前
Python中的简单爬虫
爬虫·python·信息可视化