This is the HTML structure:
Element link = doc.select("div.subtabs p").first();
That does not seem to work. How do I select that p
?
The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
It matches links with href attributes whose values start with the given string.
The DIV with the class="subtabs" is not in fact the parent of the p
element but instead is the sibling of p
. To retrieve the p
, you'll need to first get a reference to the parent DIV that has the id="content":
Element link = doc.select("div#content > p").first();
Additionally, you'll need the >
symbol to indicate that you're selecting a child of div#content.
parent > child: child elements that descend directly from parent, e.g. div.content > p finds p elements; and body > * finds the direct children of the body tag
If you get stuck with a JSOUP CSS selector in the future, check out the JSOUP Selector Syntax cookbook, which has some nice examples and explanations.
div#content p
. It is not a child of .subtabs
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With