在php中使用Xpath从网页中提取数据

在php中使用Xpath从网页中提取数据,php,html,regex,xpath,Php,Html,Regex,Xpath,我需要从中提取数据。 该网页包含评论、评论标题、发现有用的评论数量和评级(星号),我需要提取这些内容 现在我面临的问题是,我只能检索评论以及页面中第一个出现的评论(它不会移动到下一个评论) 我无法检索评论标题,因为它在html中具有不同的对象id ex:(在这种情况下,我可以使用regex作为对象id吗?) 它现在只显示第一个的原因是因为您只为->item(0)选择了一个选项,您需要在它们之间循环。此外,要打印标记内的元素,您可以使用nodeValue(您尝试了textContent,但它不存在

我需要从中提取数据。 该网页包含评论、评论标题、发现有用的评论数量和评级(星号),我需要提取这些内容

  • 现在我面临的问题是,我只能检索评论以及页面中第一个出现的评论(它不会移动到下一个评论)

  • 我无法检索评论标题,因为它在html中具有不同的对象id

  • ex:(在这种情况下,我可以使用regex作为对象id吗?)


    它现在只显示第一个的原因是因为您只为
    ->item(0)
    选择了一个选项,您需要在它们之间循环。此外,要打印标记内的元素,您可以使用
    nodeValue
    (您尝试了
    textContent
    ,但它不存在)

    下面的代码在表中打印10条评论,包括评级(星级)、标题和内容:

    $url = "https://www.trustpilot.co.uk/review/www.currys.co.uk";
    $html = file_get_contents( $url);
    libxml_use_internal_errors( true);
    $doc = new DOMDocument; $doc->loadHTML( $html);
    $xpath = new DOMXpath( $doc);
    //get all ratings where <meta itemprop="ratingValue">
    $ratings = $xpath->query('//meta[@itemprop="ratingValue"]');
    //get all headings where <h3 class="review-title en h4">
    $headings = $xpath->query( '//h3[@class="review-title en h4"]');
    //get all content
    $node = $xpath->query( '//div[@itemprop="reviewBody"][@class="review-body"]');
    
    $table = "<table border=1>";
    for($i=0;$i<10;$i++){
    $table .= '<tr>
               <td>Star: '.str_repeat("*",$ratings->item($i)->getAttribute('content')).'</tr>
               <td>'.$headings->item($i)->nodeValue.'</tr>
               <td>'.$node->item($i)->nodeValue.'</tr>
               </tr>';
    }
    $table .= '</table>';
    echo $table;
    
    $url=”https://www.trustpilot.co.uk/review/www.currys.co.uk";
    $html=文件内容($url);
    libxml\u使用\u内部错误(true);
    $doc=新文档$doc->loadHTML($html);
    $xpath=新的DOMXpath($doc);
    //在哪里获得所有收视率
    $ratings=$xpath->query('//meta[@itemprop=“ratingValue”]');
    //把所有的标题都放在哪里
    $headers=$xpath->query('//h3[@class=“review title en h4”]');
    //获取所有内容
    $node=$xpath->query('//div[@itemprop=“reviewBody”][@class=“reviewBody”]”);
    $table=“”;
    对于($i=0;$iitem($i)->getAttribute('content')。'
    “.$headers->item($i)->nodeValue”
    “.$node->item($i)->nodeValue”
    ';
    }
    $table.='';
    echo$表;
    
    @我能找到Kittenz,非常感谢你的帮助。我正在对这些评论进行分析。我只剩下两个疑问了。1.我们是否可以计算评级,例如:1星=1表示不好,5星=5表示非常好。2.该公司共有794条评论,但我可以在前面提到的for循环中使用最大值20。我们可以通过一些处理得到它吗?@我可以有Kittenz,一个小的更新。我已经得到了评分部分。我唯一正在努力的是获得评论计数(总共有794条评论被发布,但我只能循环到for循环中的值为20)以提供for循环。是否有可能使它循环通过所有评论?@vds似乎他们使用AJAX加载更多页面,但是,似乎您可以通过更改URL来访问每个页面:对于第二个页面,总共有40个页面,您可以将上述代码放入for循环中40次,并不断增加URL中的
    页面
    。如果你需要帮助,请告诉我。@Kittenz在我这里,谢谢你。正如您所说,我继续了,但程序在某个时间后停止了,出现了以下错误“第5行的C:\wamp\www\auth\truspilot.php中超过了30秒的最大执行时间”。第5行是“$html=file_get_contents($url);”。它只能执行一段时间?@vds你是对的,看起来最多只能处理8-10次。一次加载所有列表是不可能的,如果您需要在一个页面中加载列表,请像网站那样使用AJAX。当您使用AJAX单击“加载更多”时,请向PHP发送请求以加载下一页的内容。
    $url = "https://www.trustpilot.co.uk/review/www.currys.co.uk";
    $html = file_get_contents( $url);
    libxml_use_internal_errors( true);
    $doc = new DOMDocument; $doc->loadHTML( $html);
    $xpath = new DOMXpath( $doc);
    $node = $xpath->query( '//div[@itemprop="reviewBody"][@class="review-body"]')->item( 0);
    echo $node  >textContent;
    
    $url = "https://www.trustpilot.co.uk/review/www.currys.co.uk";
    $html = file_get_contents( $url);
    libxml_use_internal_errors( true);
    $doc = new DOMDocument; $doc->loadHTML( $html);
    $xpath = new DOMXpath( $doc);
    //get all ratings where <meta itemprop="ratingValue">
    $ratings = $xpath->query('//meta[@itemprop="ratingValue"]');
    //get all headings where <h3 class="review-title en h4">
    $headings = $xpath->query( '//h3[@class="review-title en h4"]');
    //get all content
    $node = $xpath->query( '//div[@itemprop="reviewBody"][@class="review-body"]');
    
    $table = "<table border=1>";
    for($i=0;$i<10;$i++){
    $table .= '<tr>
               <td>Star: '.str_repeat("*",$ratings->item($i)->getAttribute('content')).'</tr>
               <td>'.$headings->item($i)->nodeValue.'</tr>
               <td>'.$node->item($i)->nodeValue.'</tr>
               </tr>';
    }
    $table .= '</table>';
    echo $table;