0

我需要使用 for 循环从网站下载结果来编译它们。

(请注意,这是一个 ASP 请求,它显示带有这些参数的网页)

我写了下面的代码来得到这个:

<?php
for ($i=10; $i<500; $i++) {
$m = $i*10;

$dl = $query;
$text = file_get_contents($dl);
$doc = new DOMDocument('1.0');
$doc->loadHTML($text);
$aObj = $doc->find('Academic');
if (count($aObj) > 0)
{
   echo "<h4>Found</h4>";
   //Don't download this
}
else
{
   echo "<h4>Not found</h4>";
   //Download this
}
}
?>

但它返回几个错误。显然它不能将 ASPX 文件复制到 HTML DOM。我该怎么做呢?另外,如何下载/保存找不到字符串“下载”的页面?

我还认为我在文档中查找“下载”的方法不起作用。这样做的正确方法是什么?

4

1 回答 1

0

您尝试解析的网站包含很多错误,因此您将无法使用标准 DOMDocument 对象。您可以尝试使用 SimpleHTMLDOM ( http://simplehtmldom.sourceforge.net/ ) 或 phpQuery ( https://code.google.com/p/phpquery/ ) 等库,并希望这些库足以解析格式错误的文档。

如果您只需要一些信息,使用正则表达式和 preg_match_all ( http://www.php.net/manual/en/function.preg-match-all.php ) 来查找每次出现的“学术”可能会更容易例如。

Note, usually it is not very advisable to use regular expression when working with structured documents such as HTML since you wont be able to take advantage of the structure, but since those documents seem to contain over 300 errors and differ from each other it might be the only way.

于 2013-03-29T16:08:40.303 回答