dongruoqiong9017 2016-04-06 18:45
浏览 33

too long

I am trying to fetch product specification and feature list from flipkart. I am using bellow code but i am not able to fetch specification.

is there any thing which can make my code complete.

<?php
$url = 'http://www.flipkart.com/samsung-415-l-frost-free-double-door-refrigerator/p/itmedp6zcppxvhgh?pid=RFREDP6ZJXFY5QMK&al=Xh6p4IpEIjAx6PWgfu6yt8ldugMWZuE7%2BW7da8XnwKRuC2TkVUlPYWLhfoM4PDZcEqn50nOHN48%3D&ref=L%3A1683055601844045008&srno=p_2&query=samsung+rt42&otracker=from-search';
$response = getPriceFromFlipkart($url);
echo json_encode($response);
/* Returns the response in JSON format */
function getPriceFromFlipkart($url) {

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, "Chrome/49.0.2623.110 (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);


$regex = '/<meta itemprop="price" content="([^"]*)"/';
preg_match($regex, $html, $price);

$regex = '/<h1[^>]*>([^<]*)<\/h1>/';
preg_match($regex, $html, $title);


$regex = '/data-zoomimage="([^"]*)"/i';
preg_match($regex, $html, $image);


if ($price && $title && $image) {
    $response = array("price" => "Rs. $price[1].00", "image" => $image[1], "title" => $title[1], "status" => "200");

} else {
    $response = array("status" => "404", "error" => "We could not find the product details on Flipkart $url");
}

 return $response;
}
?>
  • 写回答

1条回答 默认 最新

  • dongtaidai0492 2018-07-12 09:32
    关注

    There is no magical regexp that will do that. You'll probably want to mix several regexes and code to get the specifications.

    A starting point could be using <div[^>]*>\s*Specifications\s*<\/div>(.*?)<div[^>]*>\s*Questions\s*and\s*Answers

    See: https://regex101.com/r/Tanr0H/4

    This will get the html response from "Specifications" up to "Questions and Answers"

    Please, see lazy vs greedy to understand how (.*?) works: What do 'lazy' and 'greedy' mean in the context of regular expressions?

    Also it may be a good idea to use some library to parse html

    How do you parse and process HTML/XML in PHP?

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line