Skip to content
Advertisement

JSoup not able to get links from html

I’m trying to get links from html of a site but unable to do so using Jsoup.

This is the HTML:

JavaScript

This is the android code that I wrote which doesn’t seem to work:

JavaScript

Can someone please help me with this? Thanks

Edit: Basically I’m trying to get those 6 links and add them to my list to use it within the app.

Edit 2:

So I found another HTML that can seems better:

JavaScript

Advertisement

Answer

As you can see, in this li definition you are including a nested div:

JavaScript

This is causing that the variable content, the HTML fragment with class anime_muti_link, to look like:

JavaScript

A similar result will be obtained even if you tidy your HTML. I used this code from one of my previous answers:

JavaScript

And this is why you are finding only three anchors.

Please, try correcting your HTML or selecting the anchor tag as the document level instead:

JavaScript

If the result obtained contains undesired links, perhaps you can try narrowing the selector used, something like:

JavaScript

If this doesn’t work, another possible alternative could be selecting the anchor elements with a data-video attribute, a[data-video]:

JavaScript

With your new test case, you can obtain the desired information with a very similar code:

JavaScript

The most important part is the definition of the selector that should be applied to the parsed document, div.heading-servers ul.servers li.server in our case.

I provided a selector with many fragments, but depending on the actual use HTML it could be simplified with ul.servers li.server or even li.server.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement