Python 爬虫html内存 re.findall 正则提取span
前言全局说明
爬虫html内存 re.findall 正则提取
一、百度首页热搜
(和百度原网页代码有修改)
需求:提取内容文字。
<ul class="s-hotsearch-content" id="hotsearch-content-wrapper"> <li class="hotsearch-item odd" data-index="0"> <span class="title-content-title">必须坚持人民至上</span> <span class="title-content-title">因平凡的你们熠熠闪光</span> <span class="title-content-title">已婚男子找王婆说媒 妻子:将离婚</span> <span class="title-content-title">凯迪拉克:泼天的流量轮到我了</span> <span class="title-content-title">爸爸穿得太显眼竟把女儿气哭</span> <span class="title-content-title">女子辅导儿子作业情绪崩溃踹断脚趾</span> </li> </ul>
实现代码:
baidu_hot.py
import re html_hot = """<ul class="s-hotsearch-content" id="hotsearch-content-wrapper"> <li class="hotsearch-item odd" data-index="0"> <span class="title-content-title">必须坚持人民至上</span> <span class="title-content-title">因平凡的你们熠熠闪光</span> <span class="title-content-title">已婚男子找王婆说媒 妻子:将离婚</span> <span class="title-content-title">凯迪拉克:泼天的流量轮到我了</span> <span class="title-content-title">爸爸穿得太显眼竟把女儿气哭</span> <span class="title-content-title">女子辅导儿子作业情绪崩溃踹断脚趾</span> </li> </ul>""" res = re.findall('<span class="title-content-title">(.*?)</span>', html_hot) print("html_hot=", html_hot) print("res=", res)
说明:
re.findall(<正则规则>, <待提取的数据>)
.*? 任意字符串内容
() 优先提取的内容,就是我们想要的内容
注意点:
- html_hot 的内容必须用 六个双引号内。
效果:
二、
三、
四、
免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。
参考、来源:
https://www.luffycity.com/ 路飞学城
2024-03-24_路飞3天/Day01/converter_1-1740_.ts.mp4 01:36:34
分类:
Python & Jython
标签:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!