【发布时间】:2012-12-17 06:43:23
【问题描述】:
我正在尝试编写一个 php 解析器来收集来自 ratemyprofessor.com 的教授评论。每个教授都有一个页面,其中包含所有评论,我想解析每个教授的站点并将 cmets 提取到 txt 文件中。 这是我到目前为止所拥有的,但是当我运行它时它没有正确执行,因为输出 txt 文件仍然是空的。可能是什么问题?
<?php
set_time_limit(0);
$domain = "http://www.ratemyprofessors.com";
$content = "div id=commentsection";
$content_tag = "comment";
$output_file = "reviews.txt";
$max_urls_to_check = 400;
$rounds = 0;
$reviews_stack = array();
$max_size_domain_stack = 10000;
$checked_domains = array();
while ($domain != "" && $rounds < $max_urls_to_check) {
$doc = new DOMDocument();
@$doc->loadHTMLFile($domain);
$found = false;
foreach($doc->getElementsByTagName($content_tag) as $tag) {
if (strpos($tag->nodeValue, $content)) {
$found = true;
break;
}
}
$checked_domains[$domain] = $found;
foreach($doc->getElementsByTagName('a') as $link) {
$href = $link->getAttribute('href');
if (strpos($href, 'http://') !== false && strpos($href, $domain) === false) {
$href_array = explode("/", $href);
if (count($domain_stack) < $max_size_domain_stack &&
$checked_domains["http://".$href_array[2]] === null) {
array_push($domain_stack, "http://".$href_array[2]);
}
};
}
$domain_stack = array_unique($domain_stack);
$domain = $domain_stack[0];
unset($domain_stack[0]);
$domain_stack = array_values($domain_stack);
$rounds++;
}
$found_domains = "";
foreach ($checked_domains as $key => $value) {
if ($value) {
$found_domains .= $key."\n";
}
}
file_put_contents($output_file, $found_domains);
?>
【问题讨论】:
-
使用像这样简单的东西 - simplehtmldom.sourceforge.net 页面的 jquery 样式解析。会让管理变得简单。顺便说一句,我在页面中找不到任何
div和id=commentsection,这就是为什么无法发布解决方案的原因。 -
你的标题有3个错误!它是解析器,而不是移相器,它是刮而不是废品。还有 PHP,而不是 PhP。
-
取出错误抑制器!
标签: php javascript html web-crawler