[英]How to fetch price and size of product from any ecommerce website Using Curl PHP
I wants to fetch Price and Size from Any ecommerce website using Product page URL I am writing here my code in which you can see how i am fetching image from any website. 我想使用产品页面URL从任何电子商务网站获取价格和尺寸,我在这里编写我的代码,您可以在其中看到我如何从任何网站获取图像。
<?php
function file_get_contents_curl($url)
{
//$ch = curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init($url);
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
curl_close($ch);
return $content;
}
//http://www.ralphlauren.com/product/index.jsp?productId=102437456
//https://www.amazon.com/U-S-Polo-Assn-Tyra-Fashion/dp/B01KY73PKU
//http://www.6pm.com/p/cole-haan-benson-camera-bag-ivory/product/8877280/color/422
//https://www.daraz.pk/prime-black-mens-slim-fit-leather-jacket-t11-5437633.html
$html = file_get_contents_curl("http://www.6pm.com/p/cole-haan-benson-camera-bag-ivory/product/8877280/color/422");
?>
<div style='display:none'><?php echo $html;?></div>
<?php
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
$keywords = '';
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description'){
$description = $meta->getAttribute('content');
}
$prop = $meta->getAttribute('property');
if($prop === 'og:image'){
$keywords = $meta->getAttribute('content');
if($keywords != '' ){?>
<input id="ogImg" type="hidden" value="<?php echo $keywords; ?>" />
<?php
}
}
}
echo "Title: $title". '<br/><br/>';
//echo "Description: $description". '<br/><br/>';
$items = $doc->getElementsByTagName('img');
for ($i = 0; $i < $items->length; $i++) {
echo $hi = $items->item($i)->getAttribute('id') ;
$bye = $items->item($i)->getAttribute('src') ;
//$hi= str_replace(' ','',$hi);
//echo gettype($hi).'type<br>';
if($hi !== ''){
//var_dump($items->item($i)->getAttribute('id'));
$newid = $hi;
?>
<img src="<?php echo $bye; ?>" />
<input type="hidden" id="myid" value="<?php echo $hi ?>" />
<?php
break;
}
// var_dump($items->item($i));
}
?>
<style type="text/css">
#imgFetch img{
border: 1px solid grey;
padding: 3px;
border-radius:10px;
}
</style>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
setTimeout(function(){
var myval = $('#myid').val();
if(myval !== undefined )
var elem = $('#'+myval).get(0).outerHTML;
var ogImg = $('#ogImg').val();
if(ogImg !== undefined ){
src = ogImg;
}else{
var src = document.getElementById(myval).src;
}
$('body').append('<div id="imgFetch"><img src="'+src+'" /></div>');
$('#imgFetch img').css('width','150px');
$('#imgFetch img').css('height','130px');
}, 0000);
</script>
what i am doing here is getting whole HTML in $html and echo also I am getting image object by using getElementByTagName function, I have got the image correctly and it works on any website url.But the problem is, I am not sure that how can i target price and size given with that image.I wants to grab whole ul element or select element if the price and size are in select element.So please tell me if there is any workaround. 我在这里所做的是在$ html中获取整个HTML并回显,我也通过使用getElementByTagName函数获取图像对象,我已经正确获取了图像并且可以在任何网站url上使用。但是问题是,我不确定如何我可以指定该图片给出的价格和尺寸吗?如果价格和尺寸在select元素中,我想获取整个ul元素或select元素。因此,请告诉我是否有解决方法。
您可以使用PHP Simple HTML DOM解析器来解析HTML元素,请访问此http://simplehtmldom.sourceforge.net/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.