【发布时间】:2017-07-20 18:33:49
【问题描述】:
我想使用产品页面 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>
我在这里所做的是在 $html 中获取整个 HTML 并回显我也正在使用 getElementByTagName 函数获取图像对象,我已经正确获取了图像并且它适用于任何网站 url。但问题是,我不是确定我如何定位该图像给出的价格和尺寸。如果价格和尺寸在选择元素中,我想抓取整个 ul 元素或选择元素。所以请告诉我是否有任何解决方法。
【问题讨论】:
-
我已经搜索了整个谷歌,我很确定谷歌上没有关于这个主题的任何内容。没有一个教程。
-
为任何网站请求解决方案是非常雄心勃勃的。但是,如果您可以将问题缩小到一个您遇到问题的示例,也许我们可以提供帮助。请see here how to create a minimal example提问。
-
谢谢,但为了方便起见,我在这里插入了整个代码。如果有人知道它是如何工作的,那么他只需将其作为 php 文件并运行它来查看我的结果。
-
你也可以在这个 url sa.edfa3ly.co/cart 上看到我想要的结果。粘贴我在代码中提供的任何链接 url 并查看结果。此网站如何获取数据。