Php 使用返回空白结果的Curl刮取站点
我想做的是用一个随机关键字在Amazon上搜索,然后我可能只会抓取前10个结果,问题是当我打印html结果时我什么也没有得到,它只是空白,我的代码在我看来还行,我在过去使用过CURL,从来没有使用过这个,我的代码:Php 使用返回空白结果的Curl刮取站点,php,Php,我想做的是用一个随机关键字在Amazon上搜索,然后我可能只会抓取前10个结果,问题是当我打印html结果时我什么也没有得到,它只是空白,我的代码在我看来还行,我在过去使用过CURL,从来没有使用过这个,我的代码: <?php include_once("classes/simple_html_dom.php"); function get_random_keyword() { $f_contents = file("keywords.txt"); return $f
<?php
include_once("classes/simple_html_dom.php");
function get_random_keyword() {
$f_contents = file("keywords.txt");
return $f_contents[rand(0, count($f_contents) - 1)];
}
function getHtml($page) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$html = curl_exec($ch);
print "html -> " . $html;
curl_close($ch);
return $html;
}
$html = getHtml("https://www.amazon.co.uk/s?k=" . get_random_keyword());
?>
理想情况下,我更愿意使用API,但据我所知,在授予访问权限之前,您需要先进行3次销售,有人能看到任何问题吗?我不知道还有什么需要检查的,非常感谢您的帮助。亚马逊正在返回以gzip编码的响应。您需要对其进行解码:
$html = getHtml("https://www.amazon.co.uk/s?k=" . get_random_keyword());
echo gzdecode($html);
您不应该绕过某些平台对添加
curl\u setopt($ch,CURLOPT\u SSL\u VERIFYPEER,false)施加的限制代码>返回大量损坏的文本,如���"�͇�&Da�我�E:�ٌ>M�� 但是这是需要处理的,我越来越接近:)谢谢。@苔丝也可以尝试输出任何错误消息:if(curl_errno($ch)){echo'curl error:'。curl_error($ch)}
谢谢!我使用了curl_setopt($ch,CURLOPT_ACCEPT_ENCODING,“gzip”)
,似乎解决了这个问题。