-2

enter image description here

In the picture above I want to extract the value Rs. 4794 from the highlighted line.

Here is what I have tried.

Document document = Jsoup.connect(url).get();

Element price = document.select("#mountRoot > div > div > main > div.pdp-details > div.pdp-description-container > div.pdp-price-info > p.pdp-selling-price > strong").first();

System.out.println(price);

But the code returns null.

Any kind of help will be greatly appreciated.

MONU KUMAR
  • 156
  • 3
  • 11
  • 3
    First of all, don't rely on your browser HTML viewer/inspector. Many elements there could be added later by JavaScript, and ware not in original server reply. Jsoup isn't browser emulator so it doesn't execute JavaScript which could add such elements. It only parses what server sent. Print call `System.out.println(document)` to see what you are actually parsing and check if it contains what you are looking for. – Pshemo Jun 16 '18 at 08:12
  • If it doesn't, then you will need to use other tools which provide browser emulation like Selenium webdriver. – Pshemo Jun 16 '18 at 08:13
  • 1
    Please don't post a picture of the XML or any other text file which might be needed by somebody who's solving your problem. Nobody will re-type it from the image. You need to provide a sample which is ready to just copy-paste to the text editor. – DevDio Jun 17 '18 at 17:50

2 Answers2

1

The web page you are trying to parse using Jsoup, updates the content dynamically.

You are getting a null value because the web page has not loaded completely. In order to get the price value, you will have to wait until the page loads. This cannot be done using Jsoup.

One of the options you have is to use Selenium. Using selenium you can wait for the page to load or wait until the element is visible. Then you can get the price value.

You can find it here to use selenium for the element to be visible. And there are many tutorials on this topic.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Abhilash
  • 435
  • 5
  • 15
  • Please add code and data as text ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). In general, code/errors/data in text format >>>> code/errors/data as an image >> nothing. Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – double-beep May 27 '19 at 16:51
0

Rely on absolute layout to find a div is bad. Try to use more general approach:

Element price = document.select("#mountRoot p.pdp-selling-price > strong").first();

To output the tag content, you use the .text() method:

System.out.println(price.text());
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51