我正在尝试在 Google 商务中获取评价。目标是通过 label 进行访问curl,然后从pane.rating.moreReviewslabel中获取值jsaction。
我如何修复下面的代码以获得curl?
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36');
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
$html = curl("https://www.google.com/maps?cid=12909283986953620003");
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'pane.rating.moreReviews';
$nodes = $finder->query("//*[contains(@jsaction, '$classname')]");
foreach ($nodes as $node) {
$check_reviews = $node->nodeValue;
$ses_key = preg_replace('/[^0-9]+/', '', $check_reviews);
}
// result should be: 166
echo $ses_key;
Run Code Online (Sandbox Code Playgroud)
如果我尝试 do var_dump($html);,我会得到:
string(348437) " "
Run Code Online (Sandbox Code Playgroud)
这个数字在每次页面刷新时都会发生变化。
如何查找 CID - 如果您在 Google 地图上有营业的商家:
\n或使用此工具: https: //ryanbradley.com/tools/google-cid-finder/
\nludocid\\\\u003d16726544242868601925\\\nRun Code Online (Sandbox Code Playgroud)\n\n\n提示:在 CSS 中使用“.quote”类来设置输出样式
\n
<?php\n/*\n\n Get Google-Reviews with PHP cURL & without API Key\n=====================================================\n\nHow to find the CID - If you have the business open in Google Maps:\n- Do a search in Google Maps for the business name\n- Make sure it\xe2\x80\x99s the only result that shows up.\n- Replace http:// with view-source: in the URL\n- Click CTRL+F and search the source code for \xe2\x80\x9cludocid\xe2\x80\x9d\n- CID will be the numbers after \xe2\x80\x9cludocid\\\\u003d\xe2\x80\x9d and till the last number\n\nor use this tool: https://pleper.com/index.php?do=tools&sdo=cid_converter\n\nExample\n-------\n```TXT\nludocid\\\\u003d16726544242868601925\\\n```\n\n> HINT: Use the class ".quote" in you CSS to style the output\n\n###### Copyright 2019 Igor Gaffling\n\n*/\n\n$cid = \'16726544242868601925\'; // The CID you want to see the reviews for\n$show_only_if_with_text = false; // true OR false\n$show_only_if_greater_x = 0; // 0-4\n$show_rule_after_review = false; // true OR false\n$show_blank_star_till_5 = true; // true OR false\n/* ------------------------------------------------------------------------- */\n\n$ch = curl_init(\'https://www.google.com/maps?cid=\'.$cid);\ncurl_setopt($ch, CURLOPT_USERAGENT, "Mozilla / 5.0 (Windows; U; Windows NT 5.1; en - US; rv:1.8.1.6) Gecko / 20070725 Firefox / 2.0.0.6");\ncurl_setopt($ch, CURLOPT_TIMEOUT, 60);\ncurl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);\ncurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\ncurl_setopt($ch, CURLOPT_COOKIEJAR, \'cookies.txt\');\n$result = curl_exec($ch);\ncurl_close($ch);\n$pattern = \'/window\\.APP_INITIALIZATION_STATE(.*);window\\.APP_FLAGS=/ms\';\nif ( preg_match($pattern, $result, $match) ) {\n $match[1] = trim($match[1], \' =;\'); // fix json\n $reviews = json_decode($match[1]);\n $reviews = ltrim($reviews[3][6], ")]}\'"); // fix json\n $reviews = json_decode($reviews);\n //$customer = $reviews[0][1][0][14][18];\n //$reviews = $reviews[0][1][0][14][52][0];\n $customer = $reviews[6][18]; // NEW IN 2020\n $reviews = $reviews[6][52][0]; // NEW IN 2020\n}\nif (isset($reviews)) {\n echo \'<div class="quote"><strong>\'.$customer.\'</strong><br>\';\n foreach ($reviews as $review) {\n if ($show_only_if_with_text == true and empty($review[3])) continue;\n if ($review[4] <= $show_only_if_greater_x) continue;\n for ($i=1; $i <= $review[4]; ++$i) echo \'\xe2\xad\x90\'; // RATING\n if ($show_blank_star_till_5 == true)\n for ($i=1; $i <= 5-$review[4]; ++$i) echo \'\xe2\x98\x86\'; // RATING\n echo \'<p>\'.$review[3].\'<br>\'; // TEXT\n echo \'<small>\'.$review[0][1].\'</small></p>\'; // AUTHOR\n if ($show_rule_after_review == true) echo \'<hr size="1">\';\n }\n echo \'</div>\';\n}\nRun Code Online (Sandbox Code Playgroud)\n来源: https: //github.com/gaffling/PHP-Grab-Google-Reviews
\n| 归档时间: |
|
| 查看次数: |
6387 次 |
| 最近记录: |