【问题标题】:CURLOPT_RETURNTRANSFER returns HTML in stringCURLOPT_RETURNTRANSFER 在字符串中返回 HTML
【发布时间】:2018-12-21 12:02:00
【问题描述】:

我正在尝试使用 CURL DOMDocument 或 Xpath 解析 HTML,但 CURLOPT_RETURNTRANSFER 总是以字符串形式返回 url 的 HTML,这使得解析的 HTML 无效

返回的输出:

string(102736) "<!DOCTYPE html>


    <html itemscope itemtype="http://schema.org/QAPage" class="html__responsive">

    <head>

        <title>html - PHP outputting text WITHOUT echo/print? - Stack Overflow</title>
        <link rel="shortcut icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
        <link rel="apple-touch-icon image_src" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
        <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
        <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">"

PHP 狙击查看输出

$cc = $http->get($url);
var_dump($cc);

使用的 CURL 库: https://github.com/seikan/HTTP/blob/master/class.HTTP.php

当我删除 CURLOPT_RETURNTRANSFER 时,我看到没有字符串 (102736) 的 HTML,但即使我没有请求它也会回显 url(参考:curl_exec printing results when I don't want to

这是我用来解析 html 的 PHP 狙击:

  $cc = $http->get($url);
  $doc = new \DOMDocument();
  $doc->loadHTML($cc);

  // all links in document
  $links = [];
  $arr = $doc->getElementsByTagName("a"); // DOMNodeList Object
  foreach($arr as $item) { // DOMElement Object
    $href =  $item->getAttribute("href");
    $text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue));
    $links[] = [
      'href' => $href,
      'text' => $text
    ];
  }

有什么想法吗?

【问题讨论】:

    标签: php html curl


    【解决方案1】:

    检查返回值 -

    print_r($cc);
    

    您可能会发现输出是一个数组(如果代码运行成功)。从库源来看,get()的返回是……

    return [
        'header' => $headers,
        'body'   => substr($response, $size),
    ];
    

    因此您需要将负载线更改为...

    $doc->loadHTML($cc['body']);
    

    更新:

    作为上述示例并将此问题用作要处理的页面...

    $cc = $http->get("https://stackoverflow.com/questions/51319473/curlopt-returntransfer-returns-html-in-string/51319585?noredirect=1#comment89619183_51319585");
    $doc = new \DOMDocument();
    libxml_use_internal_errors(true);
    $doc->loadHTML($cc['body']);
    
    // all links in document
    $links = [];
    $arr = $doc->getElementsByTagName("a"); // DOMNodeList Object
    foreach($arr as $item) { // DOMElement Object
        $href =  $item->getAttribute("href");
        $text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue));
        $links[] = [
            'href' => $href,
            'text' => $text
        ];
    }
    
    print_r($links);
    

    输出...

    Array
    (
        [0] => Array
            (
                [href] => #
                [text] => 
            )
    
        [1] => Array
            (
                [href] => https://stackoverflow.com
                [text] => Stack Overflow
            )
    
        [2] => Array
            (
                [href] => #
                [text] => 
            )
    
        [3] => Array
            (
                [href] => https://stackexchange.com/users/?tab=inbox
    ...
    

    【讨论】:

    • 我遵循了你的解决方案$doc-&gt;loadHTML($cc['body']);,但它仍然在字符串var_dump中返回它或以这种方式查询它:if (is_string($cc)) {echo "yes";},这一切都表明它是一个字符串而不是普通的HTML。
    • 我已经更新了代码示例,并使用此页面作为带有示例输出的 url 进行了测试运行。
    • 非常感谢,您的回答很有帮助,但是 DOMDocument 可以像 Jquery 这样基于 css 选择器获取元素吗?因为我想基于特定的类来定位href。
    • 您必须使用 XPath,stackoverflow.com/questions/8680721/… 可能会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 2016-01-18
    • 1970-01-01
    • 2023-04-06
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多