我正在尝试使用这段代码获取网站的产品图片:
<?php
$url="http://www.akasa.com.tw/update.php?tpl=product/cpu.gallery.tpl&type=Fanless Chassis&type_sub=Fanless Mini ITX&model=A-ITX19-A1B";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING, "");
$pagebody=curl_exec($ch);
curl_close ($ch);
$html=str_get_html($pagebody);
print_r($html);
PHPStorm 让我读取变量,$pagebody 得到这个值:
<html><head><title>Request Rejected</title></head><body>The requested URL was rejected. If you think this is an error, please contact the webmaster. <br><br>Your support ID is: 4977197659118049932</body></html>
当我使用浏览器时,我可以完美地看到页面,页面源也为我提供了我需要的所有不错的信息,但我想自动从中抓取一些图像。知道如何找出我需要使用 cURL 发送哪些信息,以便网站不会将我视为机器人(我猜这就是问题所在),或者如何找到此类问题的解决方案?
最佳答案
基本上,您需要对查询字符串参数进行编码,以便所有特殊字符都能正确表示到 url 中。您可以使用http_build_query
为此,您的 url 构造可能如下所示:
$url = implode('?', [
'http://www.akasa.com.tw/update.php',
http_build_query([
'tpl' => 'product/cpu.gallery.tpl',
'type' => 'Fanless Chassis',
'type_sub' => 'Fanless Mini ITX',
'model' => 'A-ITX19-A1B',
])
]);
然后是其余的代码。
关于php - cURL 抓取给我 'Request Rejected' 请求的 URL 被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47612138/