3

I am building a iOS and Android App which scans barcode and shows the product page of that book from a retailer website. But now, I only want to get the price from that product page not the whole page.

How is it possible to extract the price of a product from page, as RedLaser does with it's own App.

Product page: http://goo.gl/rDxAg Price: Rs 321

I want something like this and it can be implemented on iOS and Android, without using external server.

I am a newbie, so any help will be highly appreciated.

Ravindra S
  • 6,302
  • 12
  • 70
  • 108
Samrat Mazumdar
  • 2,641
  • 4
  • 19
  • 28
  • 1
    have you tried contacting the retailers asking if there is an API available? – rlemon Apr 19 '12 at 17:53
  • 5
    Quick searching shows that Flipkart does not have a (public) API you can use to access product information like pricing. You can always resort to data scraping, meaning you will pull the product's page, and find the relevant information, but that is not allowed by their own [terms of use](http://www.flipkart.com/s/terms). Of course if you want to go against those terms, that's all on you. – wkl Apr 19 '12 at 17:57

6 Answers6

6

If official API from the website is not available, then you have to parse downloaded html to get the data you want. There are many third-party html parser libraries available for both iOS and Android.

For iOS, check out parsing HTML on the iPhone.

For Android, check out Parse HTML in Android.

There are some code sample in both links show you how to to this.

Hope that helps.

Community
  • 1
  • 1
yorkw
  • 40,926
  • 10
  • 117
  • 130
3

A jsFiddle Demo is provided after this brief introduction.

The current Product Page you are using contains too much data just to get the price.

It's better to use the Flipkart.com Mobile Website for Books since that is faster to load up.

Reference 1: http://www.flipkart.com/m/books

Since you app must already be using the pid number for the book, you can query a mobile webpage search! The link in your Question is for a book with pid of 9780224060875

Reference 2: http://www.flipkart.com/m/search-all?query=9780224060875

On that page you can see the Book Price is inside the Span Tag with Class Name of sp.

<!-- Fragment of product price format -->
<div id="productpage-price">
 <p>
     Price:  <del> Rs. 350</del>
  <span class="sp">Rs. 263</span>
 </p>
</div>

Then, using jQuery, you can get the price data you need like so:

// Begin section to show random methods to use HTML values

    // Get the HTML of  "Rs. 263" and store it in variable as a string.
    var priceTextAndLabel = $('#productpage-price').find('span.sp').text();

    // Get the HTML of  "Rs. 263" and slice off the first 4 characters of "Rs. " leaving "263" only.
    // Adjust the .slice() if possiable that number is after decimal point. Example: "Rs.1000"
    var priceText = $('#productpage-price').find('span.sp').text().slice(4);

    // As above but convert text string of "263" to a number (to allow JavaScript Math if req.).
    // The value 10 seen below reflects decimal base 10 (vs, octal(8) example) for .parseInt();
    var priceNumber = parseInt($('#productpage-price').find('span.sp').text().slice(4),10);

    // Firefox with Firebug Console will show BLACK characters for "Rs. 263" since it's a "string".
    console.log( priceTextAndLabel );

    // Firefox with Firebug Console will show BLACK characters for "263" since it's a "string".
    console.log( priceText );

    // Firefox with Firebug Console will show BLUE characters for "263" since it's a "number".
    console.log( priceNumber );

// End section to show random method to use HTML values

OK, now for the critical part... the part you've been waiting for... and that is how to use the flipkart.com Search URL in your goal (or even the webpage).

The sad answer is you can't. Not only do they forbid it, they block it. That means you can not iframe the webpage or even use AJAX to load the Search URL.

To illustrate the above failure, here's a jsFiddle Demo that, when viewed with the browsers console, will show nothing was obtained after the AJAX connection has completed.

Reference 3: jsFiddle flipkart.com Demo


Recommended solution: There's only one real choice here. Use a book store that has a API available to use. That API, with a possible API Key for privileged access, will allow you to become a legitimate store representative.

Perhaps they will eventually have a API to offer. Right now, they have a Mobile App Store for there MP3 collection. Seeing how MP3 can reflect Audio Books, it might be a matter of time before they offer a Mobile App Store for Books as well.

arttronics
  • 9,957
  • 2
  • 26
  • 62
1

I'm working in a eCommerce, and sometimes for some CSV I need to grab data from the suppliers site, you can write a routine that for some site use an element in this case you can found the price here:

xpath: //div[3]/div[2]/div/div/div/span

like this example with Selenium and Perl:

open (INFO, '>>file.csv') or die "$!";  
my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                    port => 4444, 
                                    browser => "*chrome", 
                                    browser_url => "http://www.example.com/page.htm" );
$sel->open_ok("/page.htm");
$sel->click_ok("//table[2]/tbody/tr/td/a/img");
$sel->wait_for_page_to_load_ok("30000");
my $price = $sel->get_text("//div[3]/div[2]/div/div/div/span");
print INFO ("$price\n");
$sel->go_back_ok();

# Close file
close (INFO);

You can use a similar function for grab the data, or using another solution for webscraping

fdicarlo
  • 450
  • 1
  • 5
  • 10
1

One you get the url of the product page, to extract the price you can use Nokogiri

You first need to get the page contents and then use some method to get the price. You can do this by CSS or xpath

From Nokogiri's basic examples:

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open('http://www.YOUR_URL_HERE.com'))
price = doc.at_xpath("//span[@id='fk-mprod-our-id']").text
Christian
  • 1,258
  • 10
  • 11
1

You could use an API if provided by the retailer. Search for it!
And if there isn't an API available, you could request the page from the retailers server and parse the HTML as XML to get the element which contains the price. However, that could be broken if the retailer changes it's site. Also, ask if he allows you to use his prices.

s1m0n
  • 7,825
  • 1
  • 32
  • 45
1
<span class="price final-price our fksk-our" id="fk-mprod-our-id">
   Rs.
   <span class="small-font"> </span>
   315
</span>

I noticed this HTML is there for your Price tag.

I will suggest you to use jSoup. Download from here

Now using this library, parsing is way easier, all you have to do is.

 Document doc = null;

    try{
        doc = Jsoup.connect("You page URL comes here").get(); // get url contents
    }catch(IOException e){
         // Handle exception here.
    }

 String priceHtml = doc.select("#fk-mprod-our-id").get(0).html().trim(); // get specific tag
 System.out.println("html fetched: "+priceHtml); //print to check if right tag is selected
 priceHtml = priceHtml.replace("((<span(.)*?>)(.)*(</span>))", ""); // replace inner span using regex.
 System.out.println("My Price tag: "+priceHtml); 

I haven't tested above code, but it must work. it may contain small error. but with a little effort you can get it working.

Parsing data sometimes takes time. You must do it in background. When in background parsing gets complete, post data to your UI thread.

Edit:

surround your connect call with a try catch.

and make sure you have set following permissions in your androidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Aqif Hamid
  • 3,511
  • 4
  • 25
  • 38