【问题标题】:Use python to find html or js tags. (regex?)使用 python 查找 html 或 js 标签。 (正则表达式?)
【发布时间】:2014-03-26 20:16:10
【问题描述】:

我也对使用正则表达式以外的其他解决方案持开放态度。 检查尖括号就足够了吗? 有什么建议么?谢谢!

编辑:我需要的不是解析 html 标签,而是检查它是否有这些标签

【问题讨论】:

标签: python html regex


【解决方案1】:

您可以使用BeautifulSoup 解析器并通过迭代BeautifulSoup 对象并检查是否至少有一个Tag 元素来检查是否有任何标签:

from bs4 import BeautifulSoup, Tag

l = ['test', 'test <br>', '<br>']
for item in l:
    soup = BeautifulSoup(item, 'html.parser')
    print item, any(isinstance(element, Tag) for element in soup)

打印:

test False
test <br> True
<br> True

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我强烈推荐 lxml.html 做任何关于解析(xml、html、xhtml ...)的事情 要了解整个想法,只需快速查看这些graphs,您就会知道我在说什么;)

    如需更详细的比较,请查看here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      相关资源
      最近更新 更多