Unable to get the html content of a page, which contains data from external api

Avatar

Avatar
Ignite 5
Level 2
himasreep445197
Level 2

Likes

4 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Ignite 5
Ignite 3
Ignite 1
Give Back 3
Give Back
View profile

Avatar
Ignite 5
Level 2
himasreep445197
Level 2

Likes

4 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Ignite 5
Ignite 3
Ignite 1
Give Back 3
Give Back
View profile
himasreep445197
Level 2

27-09-2020

Hi,

 

When I am trying to access the html content of a page using jsoup, I am unable to get the content which is referring form api. Can someone help me in getting the content, which is coming from external api, in java.

 

Thanks in advance.

Replies

Avatar

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

28-09-2020

Hi @himasreep445197 

 

Lets say you have input stream of the page getting from external api, then using below code snippet we will be able access page content, elements & iterate through the elements.

 

InputStream inputStream; //fetch from external api;
String htmlContent = IOUtils.toString(inputStream);
Document document = Jsoup.parse(htmlContent);
Elements imageElements = document.getElementsByTag("img");
Elements metaElements = document.getElementsByTag("meta");
Elements headElement = document.getElementsByTag("head");
Elements scriptElements = document.getElementsByTag("script");

scriptElements.forEach(
scriptElement ->
{
String scriptPath = scriptElement.attr("src");
}
);