English 中文(简体)
将Json 和PHP 一起除去
原标题:scraping JSON with PHP
I have done a lot of HTML scraping using Xpath. But now I have to scrape some JSON and don t know how to do that. The source I want to scrape is : { "ASIN" : "B00DR4LYHY", "FeatureName" : "price_feature_div", "Type" : "JSON", "Value" : { "content" : {"price_feature_div":"
Price: $37.60 & FREE Shipping
"} } } I get this code from: $URL = http://www.amazon.com/gp/twister/ajaxv2?sid=188-4344403-7969026&ptd=OUTERWEAR&json=1&dpxAjaxFlag=1&sCac=1&isUDPFlag=1&twisterView=glance&ee=2&pgid=apparel_display_on_website&sr=1-3&nodeID=1036592&rid=0Q05FXGQJSA20X44DJVG&parentAsin=B00DR4LUQY&enPre=1&qid=1413775191&dStr=size_name%2Ccolor_name&auiAjax=1&storeID=apparel&psc=1&asinList=B00DR4LYHY&isFlushing=2&id=B00DR4LYHY&prefetchParam=0&mType=full&dpEnvironment=softlines ; What I need to get is the price ($37.60) The code I m using , as provided from Venkata is: $URL = http://www.amazon.com/gp/twister/ajaxv2?sid=188-4344403-7969026&ptd=OUTERWEAR&json=1&dpxAjaxFlag=1&sCac=1&isUDPFlag=1&twisterView=glance&ee=2&pgid=apparel_display_on_website&sr=1-3&nodeID=1036592&rid=0Q05FXGQJSA20X44DJVG&parentAsin=B00DR4LUQY&enPre=1&qid=1413775191&dStr=size_name%2Ccolor_name&auiAjax=1&storeID=apparel&psc=1&asinList=B00DR4LYHY&isFlushing=2&id=B00DR4LYHY&prefetchParam=0&mType=full&dpEnvironment=softlines ; $page = file_get_contents($URL); $decoded = json_decode($page); $html = $decoded->Value->content->price_feature_div; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); //frem dom method $elements = $dom->getElementById("priceblock_ourprice")->item(0); //OR use extract it from xpath like below line $priceNode = $xpath->query("//*[@id= priceblock_ourprice ]"); if (!is_null($elements)) { //$priceNode = $elements->item(0); $ourPrice = $priceNode; echo $ourPrice; } I think the best would be to use REGEX but what should the expression look like?
问题回答
Extraction with PHP $json_string = {"ASIN" : "B00DR4LYHY","FeatureName" : "price_feature_div","Type" : "JSON","Value" : {"content" : {"price_feature_div":"
Price: $37.60 & FREE Shipping
"}}} ; $decoded = json_decode($json_string); $html = $decoded->Value->content->price_feature_div; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); //frem dom method $elements = $dom->getElementById("priceblock_ourprice")->item(0); //OR use extract it from xpath like below line //$priceNode = $xpath->query("//*[@id= priceblock_ourprice ]"); if (!is_null($elements)) { $priceNode = $elements->item(0); $ourPrice = $priceNode; echo $ourPrice; } Extraction in frontend (I used jQuery in below solution) var jsonObj={ "ASIN" : "B00DR4LYHY", "FeatureName" : "price_feature_div", "Type" : "JSON", "Value" : { "content" : {"price_feature_div":"
Price: $37.60 & FREE Shipping
"} } }; //using jQuery we extracted the price var ourPrice = $(jsonObj.Value.content.price_feature_div).find("#priceblock_ourprice").text(); console.log(ourPrice);//"$37.60" is the value you can see in the browser-console Note: I found syntax error at "price_feature_div" html value(in JSON value it should be in single line even it is HTML string). Noticed two line breaks in the HTML.
I think the best would be to use REGEX but what should the expression look like In some cases the regex works better than xpath (for limited size unstructured html text snippets). So you just get the raw data and sticking to the $ you get what you want. $page = file_get_contents($URL); $pattern = /$[d.]+/ ; $preg_match($page, $pattern, $matches); echo price = , $matches[0]; See the working demo.




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签