【问题标题】:HTTP 500 error in simple PHP web crawler简单 PHP 网络爬虫中的 HTTP 500 错误
【发布时间】:2013-04-02 15:55:11
【问题描述】:

我正在尝试运行一个指向一个 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);
?>

【问题讨论】:

  • 您正在从正在爬行的东西中获得 500 分?或者此代码在您的服务器上生成 500?如果是您的服务器,请检查服务器的错误日志 - 它将包含有关 500 的更多详细信息。

标签: php web-crawler


【解决方案1】:

替换:

crawl(http://the-irf.com/hello/hello6.html);

与:

crawl('http://the-irf.com/hello/hello6.html');

URL 是一个文本字符串,因此必须用引号引起来。


关于stristr 的问题:

返回从第一次出现 needle 到结束的所有 haystack。

所以,你的代码:

$content = stristr( $content, "body" );

将返回从body 开始并包括第一次出现的$content

【讨论】:

  • 谢谢。这是因为 file_get_contents 要求 URL 周围有引号,对吧?
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 2017-01-26
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多