Good day.

I am writing a site parsing on jsoup, as a result I get the required string of the form

<p class="name"><a href="/level/1/book/22446/sr/1/" class="js-serp-metrika" data-url="/book/22446" data-id="22446" data-type="book">Название</a> <span class="year">2015</span></p> 

Stuck on the fact that it is impossible to pull out the value of the data-type and data-url attributes. Tried everything, an empty string is displayed. Site jsoup read to holes.

Tried so

 Elements named = doc.select("p.name"); for (Element el : named) System.out.println(el.select("data-url").attr("data-url")); 

so

 System.out.println(el.select("data-url")); 

so

 System.out.println(el.attr("data-url")); 

    1 answer 1

    The p element does not have these attributes. These attributes are for element a . This code will give an attribute

     Elements named = doc.select("p.name > a"); System.out.println(named.first().attr("data-id")); 
    • And how now to pull out the contents of the year class? It is located in the p element. Is it possible to combine selectors p.name> a and p.name? - Tariel
    • @Tariel ask a new question - Mikhail Vaysman