【发布时间】:2012-03-16 22:33:38
【问题描述】:
好的,所以我正在用 PHP 编写一个应用程序来检查我的网站是否所有链接都有效,所以如果需要我可以更新它们。
我遇到了一个问题。我尝试使用 SimpleXml 和 DOMDocument 对象来提取标签,但是当我使用示例站点运行应用程序时,如果我使用 SimpleXml 对象类型,通常会出现大量错误。
那么有没有一种方法可以像使用 SimpleXml 一样简单地扫描 html 文档中的 href 属性?
<?php
// what I want to do is get a similar effect to the code described below:
foreach($html->html->body->a as $link)
{
// store the $link into a file
foreach($link->attributes() as $attribute=>$value);
{
//procedure to place the href value into a file
}
}
?>
所以基本上我正在寻找一种方法来执行上述操作。问题是我目前对如何处理我得到的带有 html 代码的字符串感到困惑......
为了清楚起见,我使用以下原始方式获取 html 文件:
<?php
$target = "http://www.targeturl.com";
$file_handle = fopen($target, "r");
$a = "";
while (!feof($file_handle)) $a .= fgets($file_handle, 4096);
fclose($file_handle);
?>
任何信息以及任何其他可以更优雅地解决上述问题的语言替代方案(python、c 或 c++)都会很有用
【问题讨论】:
标签: php web-crawler html-parsing simplexml domdocument