【发布时间】:2012-02-15 13:26:16
【问题描述】:
编辑:我添加了一个适用于这种情况的解决方案。
我想从页面中提取一个表,并且我想(可能)使用 DOMDocument 和 XPath 来执行此操作。但如果你有更好的主意,请告诉我。
我的第一次尝试是这样的(显然是错误的,因为它会得到第一个关闭表标记):
<?php
$tableStart = strpos($source, '<table class="schedule"');
$tableEnd = strpos($source, '</table>', $tableStart);
$rawTable = substr($source, $tableStart, ($tableEnd - $tableStart));
?>
我很难,这可能可以通过 DOMDocument 和/或 xpath 解决...
最后,我想要标签(在本例中为标签)和标签之间的所有内容。所以所有的 HTML,不仅仅是值(例如,不仅仅是“价值”,而是“价值”)。还有一个“捕获”...
- 该表中包含其他表。因此,如果您只搜索表格的结尾(“标签”),您可能会得到错误的标签。
- 开始标签有一个您可以识别它的类 (classname = 'schedule')。
这可能吗?
这是我想从另一个网站提取的(简化的)源代码片段:(我还想显示 html 标记,而不仅仅是值,所以整个表都带有类 'schedule')
<table class="schedule">
<table class="annoying nested table">
Lots of table rows, etc.
</table> <-- The problematic tag...
<table class="annoying nested table">
Lots of table rows, etc.
</table> <-- The problematic tag...
<table class="annoying nested table">
Lots of table rows, etc.
</table> <-- a problematic tag...
This could even be variable content. =O =S
</table>
【问题讨论】:
-
是的,使用 DOMDocument,就像这里的拆分/合并 XML 文件示例 stackoverflow.com/questions/8602503/copy-xml-attributes-php/…
-
使用 XPath 语句,例如“//table[@class='schedule']”或“//table[3]”。
-
然后呢?你能举个例子吗?因为我就是想不通:S 我一直在努力寻找整个晚上......
-
我在您提供的 html 中的任何地方都没有看到字符串“schedule”。你想要的输出到底是什么?您使用的术语不准确(“标签”、“元素”、“html 不是值”等),因此我们无法理解您的问题。
-
@FrancisAvila:我修改了我的问题。请记住,我是荷兰人,而不是 php 专家。哦,也看看我的解决方案:)
标签: php xpath screen-scraping