简体   繁体   English

如何使用Curl PHP从任何电子商务网站获取产品的价格和大小

[英]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.

相关问题 如何使用PHP从我的网站提取/获取和显示数据(从其他电子商务网站) - How to extract/fetch and show data ( from other ecommerce site ) to my website using PHP 使用文件获取内容从电子商务网站获取产品价格 - Getting a product price from an ecommerce site using file get content 需要使用xpath从电子商务网站抓取价格 - Need to scrape price from an ecommerce website using xpath 如何使用 Laravel、PHP 和 MYSQL 在电子商务网站上获取产品的编辑历史 - How to get the edit history of a product on ecommerce website using Laravel, PHP and MYSQL 如何使用cURL从网站获取特定数据,然后使用php将其保存到我的数据库中 - How to use cURL to fetch specific data from a website and then save it my database using php 如何在使用php curl提取之前确定文件大小 - how to determine file size before fetch using php curl php curl从网站获取内容不起作用 - php curl fetch content from website is not working 如何为电子商务网站创建唯一的产品页面? - How to create a unique product page for an eCommerce website? 如何使用 Google 分析从分析中获取增强型电子商务数据 - How to fetch Enhanced ecommerce data from analytics using Google analytics Reporting API - PHP 为PHP电子商务网站创建价格范围功能的想法 - Ideas to create price range function for a PHP ecommerce website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM