I'm creating a class using jsoup that will do the following:
Below is a rough working of what I am trying to do, not its very rough as I've been trying a lot of different things
public class ParsePage {
private String path;
Connection.Response response = null;
private ParsePage(String langLocale){
try {
response = Jsoup.connect(path)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.execute();
} catch (IOException e) {
System.out.println("io - "+e);
}
}
public int getSitemapStatus(){
int statusCode = response.statusCode();
return statusCode;
}
public ArrayList<String> getUrls(){
ArrayList<String> urls = new ArrayList<String>();
}
}
As you can see I can get the page status, but using the already open connection from the constructor I don't know how to get the document to parse, I tried using:
Document doc = connection.get();
But that's a no go. Any suggestions? Or better ways to go about this?
What It Is. 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.
The connect(String url) method creates a new Connection , and get() fetches and parses a HTML file. If an error occurs whilst fetching the URL, it will throw an IOException , which you should handle appropriately. The Connection interface is designed for method chaining to build specific requests: Document doc = Jsoup.
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data, using the best of HTML5 DOM methods and CSS selectors. jsoup implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do.
As stated in the JSoup Documentation for the Connection.Response type, there is a parse()
method that parse the response's body as a Document
and returns it.
When you have that, you can do whatever you want with it.
For example, see the implementation of getUrls()
public class ParsePage {
private String path;
Connection.Response response = null;
private ParsePage(String langLocale){
try {
response = Jsoup.connect(path)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.execute();
} catch (IOException e) {
System.out.println("io - "+e);
}
}
public int getSitemapStatus() {
int statusCode = response.statusCode();
return statusCode;
}
public ArrayList<String> getUrls() {
ArrayList<String> urls = new ArrayList<String>();
Document doc = response.parse();
// do whatever you want, for example retrieving the <url> from the sitemap
for (Element url : doc.select("url")) {
urls.add(url.select("loc").text());
}
return urls;
}
}
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