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

单章小说下载:

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)

结果展现:

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

相关推荐
源码之家5 小时前
计算机毕业设计:基于Python与协同过滤的美食推荐系统 Django框架 可视化 协同过滤推荐算法 菜谱 食品 机器学习(建议收藏)✅
爬虫·python·机器学习·django·毕业设计·课程设计·美食
bryant_meng5 小时前
【Spider】Finding API Keys
爬虫·图库
进击的雷神6 小时前
攻克HTML属性数据埋点与分页偏移陷阱:基于data-id属性提取的精准爬虫设计
前端·爬虫·html·spiderflow
进击的雷神6 小时前
攻克POST动态加载与字段缺失容错:基于偏移量计算的双路条件分支爬虫设计
爬虫·spiderflow
源码之家7 小时前
计算机毕业设计:基于Python的美食菜谱数据分析可视化系统 Django框架 爬虫 机器学习 数据分析 可视化 食物 食品 菜谱(建议收藏)✅
爬虫·python·数据分析·django·flask·课程设计·美食
喵手1 天前
Python爬虫实战:手把手教你Python 自动化构建志愿服务岗位结构化数据库!
爬虫·python·自动化·数据采集·爬虫实战·零基础python爬虫教学·志愿服务岗位结构数据库打造
小邓睡不饱耶1 天前
Python多线程爬虫实战:爬取论坛帖子及评论
开发语言·爬虫·python
喵手1 天前
Python爬虫实战:手把手教你如何采集开源字体仓库目录页(Google Fonts / 其他公开字体目录)!
爬虫·python·自动化·数据采集·爬虫实战·零基础python爬虫教学·开源字体仓库目录页采集
axinawang1 天前
正则表达式
爬虫·python
喵手1 天前
Python爬虫实战:手把手带你打造私人前端资产库 - Python 自动化抓取开源 SVG 图标全目录!
爬虫·python·自动化·爬虫实战·零基础python爬虫教学·前端资产库打造·采集svg图标目录