我正在尝试运行一个指向一个 url 的网络爬虫,它没有链接,代码看起来不错;但是,我收到一个 http 500 错误。
它对爬取的内容所做的只是回显它。
知道为什么吗?
<?php
error_reporting( E_ERROR );
define( "CRAWL_LIMIT_PER_DOMAIN", 50 );
$domains = array();
$urls = array();
function crawl( $url )
{
global $domains, $urls;
$parse = parse_url( $url );
$domains[ $parse['host'] ]++;
$urls[] = $url;
$content = file_get_contents( $url );
if ( $content === FALSE ){
echo "Error: No content";
return;
}
$content = stristr( $content, "body" );
preg_match_all( '/http:\/\/[^ "\']+/', $content, $matches );
// do something with content.
echo $content;
foreach( $matches[0] as $crawled_url ) {
$parse = parse_url( $crawled_url );
if ( count( $domains[ $parse['host'] ] ) < CRAWL_LIMIT_PER_DOMAIN && !in_array( $crawled_url, $urls ) ) {
sleep( 1 );
crawl( $crawled_url );
}
}
}
crawl(http://the-irf.com/hello/hello6.html);
?>