【发布时间】:2018-11-24 18:04:09
【问题描述】:
<?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 页的评论。
谢谢!
【问题讨论】:
-
用于报废:1) 选择有用的“登陆”,其中包含您需要的任何类型的寻呼机 -> 因此无需预先定义最大页面的原始值; 2)放置一些响应示例 - >对于代码的第二部分,解析数据,因为您的第一部分代码,curl链接,不提供报废的数据示例; 3)当你使用 php 并且你提到了 json,所以检查一个php.net/manual/en/function.json-decode.php 函数
-
all 25 pages- 呃,我在第 1 页上只能看到 1 条评论..
标签: php curl web-scraping trustpilot