【问题标题】:HTML parse error: Service out of order - when trying parsing a websiteHTML 解析错误:服务乱序 - 尝试解析网站时
【发布时间】:2015-09-07 10:05:55
【问题描述】:

我想解析一个网站,但是 我总是得到一个错误:服务故障。

无论我给出什么开始或结束字符串。 我也尝试使用其他 URL 并复制 其他为他们工作的用户的完整示例 但不适合我。我还尝试将大小增加到 20000。 但没有任何效果。

这是我的 php 脚本:

<?php
// URL, die durchsucht werden soll
$url = "http://cordis.europa.eu/project/rcn/85400_en.html";

// Zeichenfolge vor relevanten Einträgen
$startstring = "<div class='tech'><p>";

// bis zum nächsten html tag bzw. Zeichenfolge nach relevanten Einträgen
$endstring = "<"; 

$file = @fopen ($url,"r");

if($file)
{
    echo "URL found<br>";
}

if (trim($file) == "") {
    echo "Service out of order - File:".$file."<br>";
    } else {
    $i=0;
    while (!feof($file)) {

        // Wenn das File entsprechend groß ist, kann es unter Umständen
        // notwendig sein, die Zahl 2000 entsprechend zu erhöhen. Im Falle
        // eines Buffer-Overflows gibt PHP eine entsprechende Fehlermeldung aus.

        $zeile[$i] = fgets($file,20000);
        $i++;
    }
    fclose($file);
}

// Data filtering

for ($j=0;$j<$i;$j++) {
    if ($resa = strstr($zeile[$j],$startstring)) {
        $resb = str_replace($startstring, "", $resa);
        $endstueck = strstr($resb, $endstring);
        $resultat .= str_replace($endstueck,"",$resb);
        $resultat .= "; ";
    }
}

// Data output

echo ("Result = ".$resultat."<br>");
return $resultat;

感谢您的帮助。 提前致谢

编辑:找到 URL 并且文件有一个值:Resource id #3

【问题讨论】:

  • 你想从网站得到什么数据??
  • 我只想要简单的文字。它就像网站上的描述。如果您查看页面,它在目标下
  • $file 是一个文件句柄。你想用 trim($file) 检查什么?这将永远是空的。
  • 只需检查下面的程序,它会按预期为您提供输出。
  • @ChristianMansch 你检查程序了吗

标签: php html html-parsing screen-scraping


【解决方案1】:

试试

<?php
// URL, die durchsucht werden soll
$url = "http://cordis.europa.eu/project/rcn/85400_en.html";

$html = file_get_contents($url);

if ($html === false) {
    //Service unavailable
    echo 'Service unavailable';
    return;
}
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DomXpath($dom);

$div = $xpath->query("//*[@class='tech']")->item(0);
$output = trim($div->textContent);

// Data output

echo ("Result = " . $output. "<br>");
return $output;

【讨论】:

  • 返回 $resultat;是空白
  • 服务器现在超载 ;-)
  • 好的,非常感谢,这正是我所需要的。两种解决方案都对我有用。感谢您的努力
【解决方案2】:

使用它会得到预期的输出。

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://cordis.europa.eu/project/rcn/85400_en.html");
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, ''); 

$headers = array();
$headers[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$headers[] = 'Accept-Encoding:gzip, deflate, sdch';
$headers[] = 'Accept-Language:en-US,en;q=0.8';
$headers[] = 'Cache-Control:max-age=0';
$headers[] = 'Connection:keep-alive';
$headers[] = 'Cookie:CORDIS=14.141.177.158.1441621012200552; PHPSESSID=jrf2e3t4vu56acdkf9np0tat06; WT_FPC=id=14.141.177.158-1441621016.978424:lv=1441605951963:ss=1441604805004
Host:cordis.europa.eu';
$headers[] = 'User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36';
$headers[] = 'Host:cordis.europa.eu';
$headers[] = 'Request URL:http://cordis.europa.eu/project/rcn/85400_en.html';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
curl_close($ch);

$dom = new DOMDocument;
$dom->loadHTML($server_output);
$xpath = new DomXpath($dom);

$div = $xpath->query("//*[@class='tech']")->item(0);
$data = trim($div->textContent);
echo $data;
?>

输出

【讨论】:

    猜你喜欢
    • 2017-09-12
    • 2011-06-16
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多