Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsoup second element instead of first()

I have translated the PHP Simple HTML DOM query:

$article->find('td[id$=tdDescription] div a', 1)->plaintext;

to the jsoup query:

resultRow.select("td[id$=tdDescription] > div > a").first().text());

as you can see I am acessing the second (1) result in PHP, currently in jsoup with the .first() I am accessing the first result (0) but I would also like to access the second result (1), how would I do that?

like image 521
Dominik Avatar asked Jun 04 '11 13:06

Dominik


People also ask

Can we use XPath in Jsoup?

With XPath expressions it is able to select the elements within the HTML using Jsoup as HTML parser.

What is Jsoup element?

A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements). From an Element, you can extract data, traverse the node graph, and manipulate the HTML.

What does Jsoup parse do?

jsoup can parse HTML files, input streams, URLs, or even strings. It eases data extraction from HTML by offering Document Object Model (DOM) traversal methods and CSS and jQuery-like selectors. jsoup can manipulate the content: the HTML element itself, its attributes, or its text.


2 Answers

Use Elements#get() instead. This allows accessing elements by index.

resultRow.select("td[id$=tdDescription] > div > a").get(1).text();
like image 123
BalusC Avatar answered Nov 11 '22 05:11

BalusC


Use td[id$=tdDescription] > div > a:eq(2)selector.

like image 40
Afroz Shaikh Avatar answered Nov 11 '22 06:11

Afroz Shaikh