python - 在 python 中使用正则表达式从 HTML 中提取内容

标签 python html regex

我有这个 HTML 代码

<a class="button block left icon-phone" data-reveal="\u06f0\u06f9\u06f3\u06f6\u06f5\u06f6\u06f8\u06f1\u06f6\u06f2\u06f1"  href="#">

这是一个刺痛,我想在 data-reveal 前面提取内容。 我做了一些正则表达式,比如

p = re.compile('data-reveal=*')

但是他们没有工作。我该怎么做 ? 谢谢。

最佳答案

您正在使用 the wrong tool为了这。你应该使用像 BeautifulSoup 这样的 Html 解析器.

>>> from bs4 import BeautifulSoup
>>> doc = """<a class="button block left icon-phone" data-reveal="\u06f0\u06f9\u06f3\u06f6\u06f5\u06f6\u06f8\u06f1\u06f6\u06f2\u06f1"  href="#">"""
>>> soup = BeautifulSoup(doc, 'html.parser')
>>> print(soup.find('a').get('data-reveal'))
۰۹۳۶۵۶۸۱۶۲۱

关于python - 在 python 中使用正则表达式从 HTML 中提取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539354/

相关文章:

python - MARIADB - Python 连接问题

python - 如何加载和blit图像Python

带有 importlib 模块的 Python 多进程

html - 在html中垂直减少标题和段落之间的空间

regex - Powershell 在两行中一起搜索模式字符串

ios - 使用正则表达式删除 Objective-C 中的特定#tag

python - Shopify Python API 和纺织品库存

javascript - 如何在react中使用多个 `export default `

javascript - 仅验证一组单选按钮的最后一个单选按钮

javascript - jQuery 邮政编码验证不起作用