Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- public class ContentURL
- {
- private final String _url;
- private final String _select;
- private boolean _next = false;
- public ContentURL(String url)
- {
- this(url, "");
- }
- public ContentURL(String url, String select)
- {
- _url = url;
- _select = select;
- }
- public ContentURL next()
- {
- _next = true;
- return this;
- }
- private String result()
- {
- try
- {
- final Document doc = Jsoup.connect(_url).ignoreContentType(true).ignoreHttpErrors(true).get();
- if (_select.isEmpty())
- return doc.html();
- final Element element = doc.selectFirst(_select);
- if (_next)
- return element.nextSibling().toString();
- return element.text();
- }
- catch (Exception e)
- {
- System.out.println("Invalid 'select' format for url '" + _url + "'.");
- }
- return "";
- }
- @Override
- public String toString()
- {
- return result();
- }
- public int toInt()
- {
- final String result = result().replaceAll("[^0-9]", "");
- return result.isBlank() ? -1 : Integer.parseInt(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement