【问题标题】:How to Fetch Amazon Product Data By Php Without Api如何在没有 Api 的情况下通过 PHP 获取亚马逊产品数据
【发布时间】:2021-01-16 19:28:48
【问题描述】:

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

这是我尝试的代码示例,但它需要验证码

$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;

【问题讨论】:

  • 到目前为止你完成了吗?提供您的代码
  • I also try by cUrl/file get contents but failed。你得到什么错误?
  • 是 $curl = curl_init('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);回声 $html;这是我的代码。在输出其所需的机器人检查验证码

标签: php amazon-web-services api curl file-get-contents


【解决方案1】:

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',
)

【讨论】:

  • 谢谢。它正在工作。您还可以帮助从亚马逊搜索结果中获取数据吗?我想从搜索关键字中获取数据。示例:amazon.com/s?k=headphone
  • @SumonRana 先自己尝试一下,如果遇到困难,请尝试寻求帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多