如何在没有 Api 的情况下通过 Php 获取亚马逊产品数据

How to Fetch Amazon Product Data By Php Without Api

我想从亚马逊搜索结果和产品详情中获取数据。我找到了一些来源以通过 api 获取数据。但是我正在搜索没有 api 的获取产品信息。例如:https://www.amazon.com/Apple-MWP22AM-A-AirPods-Pro/dp/B07ZPC9QD4 这是一个亚马逊产品链接。我想获取此产品数据。我也尝试通过 cUrl/file 获取内容但失败了。请问有人可以帮忙吗?

这里是我尝试的代码示例,但它需要 Capcha

$curl = curl_init('https://www.amazon.com/gp/product/B00M0QVG3W');
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
curl_close($curl);

echo $html;

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org");

你为什么要骗他们?你不是网络浏览器,亚马逊似乎不会阻止那些没有撒谎的人..

这个:

<?php
declare(strict_types = 1);
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_ENCODING => '', // Warning: if we don't say "Accept-Encoding: gzip", the SOB's at Amazon will send it gzip-compressed anyway.
    CURLOPT_URL => 'https://www.amazon.com/Apple-MWP22AM-A-AirPods-Pro/dp/B07ZPC9QD4'
));
$html = curl_exec($ch);
@($domd = new DOMDocument())->loadHTML($html);
$xp=new DOMXPath($domd);
$product=[];
$product["productName"]=trim($domd->getElementById("productTitle")->textContent);
$product["stock"]=trim($domd->getElementById("availability")->textContent);
$prodInfo=$xp->query("//*[@id='productOverview_feature_div']//tr[contains(@class,'a-spacing-small')]");
foreach($prodInfo as $info){
    $product[trim($info->getElementsByTagName("td")->item(0)->textContent)]=trim($info->getElementsByTagName("td")->item(1)->textContent);
}
var_export($product);

打印:

array (
  'productName' => 'Apple AirPods Pro',
  'stock' => 'Temporarily out of stock.
(bunch of newlines)
We are working hard to be back in stock as soon as possible.',
  'Brand' => 'Apple',
  'Connections' => 'Wireless',
  'Model Name' => 'Apple AirPods Pro',
  'Color' => 'White',
  'Headphones Form Factor' => 'In Ear',
)