用PHP抓取 Amazon.com 网页



我试图简单地获取远程亚马逊网址的html。我有工作代码,但也许他们改变了一些东西?不确定。我花了几个小时在这里和那里尝试代码示例和插件,但没有任何效果。这是我现在拥有的,但当然它也不起作用:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $item['URL']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$output = json_decode(curl_exec($curl));
//echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
@file_put_contents($graphics_file_root.'rps/amazon/temp2.html',$output);
$html = new DOMDocument();
@$html->loadHTML($output);
@file_put_contents($graphics_file_root.'rps/amazon/temp.html',$html->saveHTML());
$temp = $html->getElementsByTagName('img');
$html = file_get_contents($item['URL']);
@file_put_contents($graphics_file_root.'rps/amazon/temp2.html',$html);
$temp = $html->getElementsByTagName('img');
echo count($temp);
print_r($temp);

这是行不通的。simple_html_dom行不通的。我找不到任何东西。

看起来我在网上找到的一些代码是特定于 json 的,删除 json-decode 修复了它:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $item['URL']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$output = curl_exec($curl);
//echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
//file_put_contents($graphics_file_root.'rps/amazon/temp2.html',$output);
$html = new DOMDocument();
@$html->loadHTML($output);
//file_put_contents($graphics_file_root.'rps/amazon/temp.html',$html->saveHTML());
$temp = $html->getElementsByTagName('img');

最新更新