【问题标题】:Parsing html to retrieve tags [duplicate]解析html以检索标签[重复]
【发布时间】: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 执行类似的任务

标签: php html parsing tags


【解决方案1】:

请参阅http://www.php.net/manual/en/class.domdocument.php 以解析 PHP 中的 DOM(例如,从方法 http://www.php.net/manual/en/domdocument.loadhtml.php 开始)。有关任何其他 API,另请参阅 http://www.php.net/manual/en/book.dom.php

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多