惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
I
InfoQ
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
C
Cisco Blogs
G
GRAHAM CLULEY
The Register - Security
The Register - Security
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
小众软件
小众软件
雷峰网
雷峰网
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
博客园 - 聂微东
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
腾讯CDC
P
Palo Alto Networks Blog
Scott Helme
Scott Helme

博客园 - pcwanli

Python批量将Word文档(.doc)转换为.docx格式的完整实现步骤 Linux下版本控制器(SVN) -命令行客户端 linux svn 命令 Qwen3-VL视频科技:内容审核系统搭建 svn update 出现Skipped 'feifazuzhi' -- Node remains in conflict处理方法 linux redis.service如何编写 深入php redis pconnect PHP中使用Redis长连接笔记 python happybase 批量读取 如何用phpredis持久化连接pconnect方法提升应用响应速度 python处理常见格式压缩包文件的全指南 Python编程:happybase读写HBase数据库 python redis zset 按分值获取记录 hbase日志如何清理 hbase日志清理 Python的time.strftime()方法 python re.sub第二参数,前值如何引用 【Python】基于python实现Windows Service程序 python解析url参数 PHP字符串分割:explode()函数详解与应用 python requests get请求禁用自动解压 python importlib动态模块加载遇到is not a package错误,解决方法。 Python正则表达式替换(re.sub)的6种典型应用场景 用Python处理HTML转义字符的5种方式 pymongo批量更新bulk_write php mongodb操作 PHP 通过 Thrift 操作 Hbase 使用thrift的php版本操作hbase数据库 PHP通过Thrift操作Hbase
提取网页源码头信息的正则表达式
pcwanli · 2026-03-27 · via 博客园 - pcwanli

来源:百度搜索

提取网页源码中的‌头信息‌(通常指 <head> 标签及其内部内容)时,可以使用正则表达式匹配从 <head> 开始到 </head> 结束之间的所有内容。以下是基于公开资料整理的常用方法和正则表达式:


‌核心正则表达式‌

  • ‌匹配整个 <head> 区块(包含标签本身)‌:

    • [\s\S]:匹配任意字符(包括换行符和空格),等价于 . 但不受 re.DOTALL 限制。
    • *?:非贪婪匹配,确保在第一个 </head> 就停止,避免跨多个 <head> 标签误匹配。
    • 改进:<head[\s\S]*?</head>,第一标签为开口,因为第一个标签可能存在属性,如:<head lang="zh">xxx</head>
  • ‌仅提取 <head> 内部的内容(不含 <head> 和 </head> 标签)‌:

    • 使用捕获组 () 提取中间内容,可通过 .group(1) 获取。

‌Python 实现示例‌

import re import requests # 获取网页源码 url = "https://example.com" response = requests.get(url) html = response.text

⚠️ 注意:若网页内容包含多个 <head> 标签(虽不符合 HTML 标准),非贪婪匹配仍可能出错。此时建议使用 ‌BeautifulSoup‌ 等 HTML 解析器更可靠 ‌5。


‌补充说明‌

  • 若需匹配 ‌<title>‌ 等具体头信息,可单独使用:

    <title>(.*?)</title> # 提取网页标题

  • 使用 re.IGNORECASE 可忽略大小写(如 <HEAD> 与 <head>)‌12。
  • 对于复杂或结构不规范的 HTML,‌正则表达式易出错‌,推荐使用专门的 HTML 解析库如 BeautifulSoup ‌5。