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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recent Announcements
Recent Announcements
博客园 - 聂微东
B
Blog RSS Feed
人人都是产品经理
人人都是产品经理
U
Unit 42
博客园 - 三生石上(FineUI控件)
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
T
Troy Hunt's Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Schneier on Security
Schneier on Security
AI
AI
T
Tailwind CSS Blog
A
About on SuperTechFans
小众软件
小众软件
云风的 BLOG
云风的 BLOG
Google Online Security Blog
Google Online Security Blog
Forbes - Security
Forbes - Security
WordPress大学
WordPress大学
Martin Fowler
Martin Fowler
Cisco Talos Blog
Cisco Talos Blog
有赞技术团队
有赞技术团队
Recorded Future
Recorded Future
MongoDB | Blog
MongoDB | Blog
B
Blog
The GitHub Blog
The GitHub Blog
IT之家
IT之家
SecWiki News
SecWiki News
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CERT Recently Published Vulnerability Notes
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Know Your Adversary
Know Your Adversary
大猫的无限游戏
大猫的无限游戏
D
Docker
I
Intezer
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
Vulnerabilities – Threatpost
C
Check Point Blog
The Register - Security
The Register - Security
Scott Helme
Scott Helme
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - 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种典型应用场景 pymongo批量更新bulk_write php mongodb操作 PHP 通过 Thrift 操作 Hbase 使用thrift的php版本操作hbase数据库 PHP通过Thrift操作Hbase
用Python处理HTML转义字符的5种方式
pcwanli · 2026-03-15 · via 博客园 - pcwanli

来源:https://cloud.tencent.com/developer/article/2047913

写爬虫是一个发送请求,提取数据,清洗数据,存储数据的过程。在这个过程中,不同的数据源返回的数据格式各不相同,有 JSON 格式,有 XML 文档,不过大部分还是 HTML 文档,HTML 经常会混杂有转移字符,这些字符我们需要把它转义成真正的字符。

什么是转义字符

在 HTML 中 <>& 等字符有特殊含义(<,> 用于标签中,& 用于转义),他们不能在 HTML 代码中直接使用,如果要在网页中显示这些符号,就需要使用 HTML 的转义字符串(Escape Sequence),例如 < 的转义字符是 &lt;,浏览器渲染 HTML 页面时,会自动把转移字符串换成真实字符。

转义字符(Escape Sequence)由三部分组成:第一部分是一个 & 符号,第二部分是实体(Entity)名字,第三部分是一个分号。 比如,要显示小于号(<),就可以写&lt;

Python 反转义字符串

Python 来处理转义字符串有多种方式,而且 py2 和 py3 中处理方式不一样,在 python2 中,反转义串的模块是 HTMLParser

# python2
import HTMLParser
>>> HTMLParser().unescape('a=1&amp;b=2')
'a=1&b=2'

Python3 把 HTMLParser 模块迁移到 html.parser

# python3
>>> from html.parser import HTMLParser
>>> HTMLParser().unescape('a=1&amp;b=2')
'a=1&b=2'

到 python3.4 之后的版本,在 html 模块新增了 unescape 方法。

# python3.4
>>> import html
>>> html.unescape('a=1&amp;b=2')
'a=1&b=2'

推荐最后一种写法,因为 HTMLParser.unescape 方法在 Python3.4 就已经被废弃掉不推荐使用,意味着之后的版本有可能会被彻底移除。

另外,sax 模块也有支持反转义的函数

>>> from xml.sax.saxutils import unescape
>>> unescape('a=1&amp;b=2')
'a=1&b=2'