【问题标题】:How can I find string with regular expressions in Python如何在 Python 中找到带有正则表达式的字符串
【发布时间】:2020-02-20 15:56:06
【问题描述】:

例如,我有这样的 html 字符串

<td align="left" nowrap="nowrap">John 23</td>

我想在'&lt;td align="left" nowrap="nowrap"&gt;''&lt;/td&gt;' 之间找到“John 23”

我想用python中的正则表达式查找

我该怎么做?

【问题讨论】:

标签: python regex search expression


【解决方案1】:

使用 BeautifulSoup 解析 HTML。正则表达式是错误的工具;它适用于本示例,但不能很好地扩展到完整文档。

>>> from bs4 import BeautifulSoup
>>> html = '<td align="left" nowrap="nowrap">John 23</td>'
>>> BeautifulSoup(html).find("td").text
'John 23'

【讨论】:

  • 非常感谢。 BeautifulSoup 是一个非常完美的库。
猜你喜欢
  • 2015-03-30
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
相关资源
最近更新 更多