简体   繁体   中英

Unable to parse img and name from amazon or flipkart pages using Jsoup

I am unable to get main image and name for products at Amazon or Flipkart using Jsoup.

My java/jsoup code for the same is:

// For amazon
Connection connection = Jsoup.connect(url).timeout(5000).maxBodySize(1024*1024*10);
Document doc = connection.get();
Elements imgs = doc.select("img#landingImage");
Elements names = doc.select("span#productTitle");

// For flipkart
Connection connection = Jsoup.connect(url).timeout(5000).maxBodySize(1024*1024*10);
Document doc = connection.get();
Elements imgs = doc.select("h1.title");
Elements names = doc.select("img.productImage.current");

Can someone please point out what am I missing here?

URLs I have used are:

http://www.flipkart.com/lenovo-yoga-2-tablet-android-10-inch/p/itmeyqkznqa2zjf5?pid=TABEYQKXWAXMSGER&srno=b_2&offer=ExchangeOffer_LenovoYoga.&ref=9ea008ab-ae95-4f52-8ef7-3ef1a54947ae

and

http://www.amazon.com/gp/product/B00LZGBU3Y/ref=s9_psimh_gw_p504_d0_i5?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=desktop-1&pf_rd_r=0ESK1KNE31TBRVC8115Q&pf_rd_t=36701&pf_rd_p=1970559082&pf_rd_i=desktop

Also, I would like to do this parsing on the front end if possible using javascript and jquery.

Is there a way to do the same?

Found out the issue.

Jsoup in GAE works when we use the URL fetch service using java.net.URL as:

private String read(String url) throws IOException
{
    URL urlObj = new URL(url);
    BufferedReader reader = new BufferedReader(new InputStreamReader(urlObj .openStream()));
    String line;
    StringBuffer sbuf = new StringBuffer();

    while ((line = reader.readLine()) != null) {
        if (line.trim().length() > 0)
            sbuf.append(line).append("\n");
    }
    reader.close();
    return sbuf.toString();
}

And then you use regular Jsoup as:

String html = read(url);
Document doc = Jsoup.parse(html);

Doing the above works very well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM