【发布时间】:2012-10-02 02:02:44
【问题描述】:
可能重复:
Count all HTML tags in page PHP
How to parse and process HTML with PHP?
我在解析信息以检索 HTML 文档中使用的 html 和元标记列表以及每个标记在文档中出现的次数时遇到问题。
例如,如果我有以下 html 文档
<head>
<a href="example.com">example1</a>
<a href="example.com">example2</a>
<a href="example.com">example3</a>
</head>
然后你会得到一个类似的列表
head tag =1
a tag =3
我正在尝试用 php 来做这件事,如果有人能给我一个很好的起点。
编辑: 我正在尝试使用 php 复制类似以下 python 代码的内容
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print "Encountered a start tag:", tag
def handle_endtag(self, tag):
print "Encountered an end tag :", tag
def handle_data(self, data):
print "Encountered some data :", data
【问题讨论】:
-
在某种意义上是的,我确实读过那篇文章,但我更难以识别何时出现 html 标签。我希望在以下链接中执行示例 19.1.1 中的 python 代码之类的操作:docs.python.org/library/htmlparser.html 但似乎无法弄清楚如何使用 php 执行类似的任务