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

单章小说下载:

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)

结果展现:

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

相关推荐
喵手11 小时前
Python爬虫实战:从零构建书籍价格情报数据库(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·构建书籍价格情报·书籍价格采集
喵手15 小时前
Python爬虫实战:基于ETag/Last-Modified的智能条件请求与流量优化!
爬虫·python·爬虫实战·零基础python爬虫教学·etag/last·modified·智能条件请求与流量优化
小恰学逆向1 天前
【爬虫JS逆向实战】关于this指向的逆向实战——某产权交易中心数据解密
爬虫
喵手1 天前
Python爬虫实战:网抑云音乐热门歌单爬虫实战 - 从入门到数据分析的完整指南!
爬虫·python·爬虫实战·网易云·零基础python爬虫教学·音乐热门采集·热门歌单采集
喵手2 天前
Python爬虫实战:节奏律动 - Billboard Hot 100 历史榜单深度采集实战!
爬虫·python·爬虫实战·零基础python爬虫教学·billboard hot·历史版单采集·采集billboard hot
喵手2 天前
Python爬虫实战:数字时光机 - 基于 Playwright 的网页全貌归档系统(HTML + 截图)(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·playwright·零基础python爬虫教学·csv导出·网页全貌归档
喵手2 天前
Python爬虫实战:自动化构建 arXiv 本地知识库 - 从 PDF 下载到元数据索引!
爬虫·python·自动化·arxiv·本地知识库·pdf下载·元数据索引
喵手2 天前
Python爬虫实战:Spotify 公开歌单爬虫实战 - 打造你的全球音乐数据库!
爬虫·python·爬虫实战·spotify·零基础python爬虫教学·公开歌单爬虫实战·全球音乐数据库
橙露2 天前
Python 异步爬虫进阶:协程 + 代理池高效爬取实战
开发语言·爬虫·python
喵手3 天前
Python爬虫实战:地图 POI + 行政区反查(合规接口) - 商圈热力数据准备等!
爬虫·python·爬虫实战·零基础python爬虫教学·行政区反查·地图poi·商圈热力数据准备