PHP-刮除所有trustpilot评论的数据

时间:2018-11-24 18:04:09

标签: php curl web-scraping trustpilot

<?php 
for ($x = 0; $x <= 25; $x++) {

$ch = curl_init("https://uk.trustpilot.com/review/example.com?languages=all&page=$x");
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout in seconds
$trustpilot = curl_exec($ch);

// Check if any errorccurred
if(curl_errno($ch))
{
     die('Fatal Error Occoured');
}

} 
?>

这段代码将获得example.com的全部25页评论,然后我要做的是将所有结果放入JSON数组或类似内容中。

我尝试使用下面的代码来检索所有名称:

<?php
$trustpilot = preg_replace('/\s+/', '', $trustpilot); //This replaces any spaces with no spaces
$first = explode( '"name":"' , $trustpilot );
$second = explode('"' , $first[1] );
$result = preg_replace('/[^a-zA-Z0-9-.*_]/', '', $second[0]);    //Don't allow special characters

?>

这显然比我预期的要难得多,没有人知道我如何将所有评论纳入JSON或任何我选择的页面的内容,例如,在这种情况下,我选择了25页的评论。 / p>

谢谢!

2 个答案:

答案 0 :(得分:1)

do not parse HTML with regex.

使用DOMDocument和DOMXPath解析em。同样,您为每个页面创建了一个新的curl句柄,但是您从未关闭它们,这是代码中的资源/内存泄漏,而且还浪费了CPU,因为您可以只是不断重复使用一遍又一遍地使用相同的curl句柄(而不是为每个页面创建一个新的curl句柄,这需要cpu),并提示:此html压缩得很好,因此您应该使用CURLOPT_ENCODING下载压缩的页面, 例如:

<?php
declare(strict_types = 1);
header("Content-Type: text/plain;charset=utf-8");
$ch = curl_init();
curl_setopt($ch, CURLOPT_ENCODING, ''); // enables compression
$reviews = [];
for ($x = 0; $x <= 25; $x ++) {
    curl_setopt($ch, CURLOPT_URL, "https://uk.trustpilot.com/review/example.com?languages=all&page=$x");
    // curl_setopt($ch, CURLOPT_POST, true);
    // curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); // timeout in seconds
    $trustpilot = curl_exec($ch);

    // Check if any errorccurred
    if (curl_errno($ch)) {
        die('fatal error: curl_exec failed, ' . curl_errno($ch) . ": " . curl_error($ch));
    }
    $domd = @DOMDocument::loadHTML($trustpilot);
    $xp = new DOMXPath($domd);
    foreach ($xp->query("//article[@class='review-card']") as $review) {
        $id = $review->getAttribute("id");
        $reviewer = $xp->query(".//*[@class='content-section__consumer-info']", $review)->item(0)->textContent;
        $stars = $xp->query('.//div[contains(@class,"star-item")]', $review)->length;
        $title = $xp->query('.//*[@class="review-info__body__title"]', $review)->item(0)->textContent;
        $text = $xp->query('.//*[@class="review-info__body__text"]', $review)->item(0)->textContent;
        $reviews[$id] = array(
            'reviewer' => mytrim($reviewer),
            'stars' => ($stars),
            'title' => mytrim($title),
            'text' => mytrim($text)
        );
    }
}
curl_close($ch);
echo json_encode($reviews, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | (defined("JSON_UNESCAPED_LINE_TERMINATORS") ? JSON_UNESCAPED_LINE_TERMINATORS : 0) | JSON_NUMERIC_CHECK);


function mytrim(string $text): string
{
    return preg_replace("/\s+/", " ", trim($text));
}

输出:

{
    "4d6bbf8a0000640002080bc2": {
        "reviewer": "Clement Skau Århus, DK, 3 reviews",
        "stars": 5,
        "title": "Godt fundet på!",
        "text": "Det er rigtig fint gjort at lave et example domain. :)"
    }
}

因为这里列出的网址只有1条评论。 4d6bbf8a0000640002080bc2是该评论的网站内部ID(可能是sql db ID)。

答案 1 :(得分:1)

请注意,由于Trustpilot条款3.3和3.5的规定,禁止从trustpilot.com抓取数据: https://legal.trustpilot.com/end-user-terms-and-conditions