Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- public class WebScrape {
- public static void main(String[] args) {
- int counter = 0;
- String currentDebt = "";
- Document doc;
- try {
- doc = Jsoup.connect("http://www.brillig.com/debt_clock/").get();
- //get the items in the img and paragraph tags
- Elements img = doc.getElementsByTag("img");
- Elements paragraphs = doc.select("p");
- //look goes through imgs to find the alts for the images. (only need the first one which contains the debt)
- for (Element el : img) {
- //only need the first alt (added a counter) there must be a better way to do this)
- if(counter != 0) {
- break;
- }
- currentDebt = el.attr("alt"); //assigns the current debt to use as a variable later on
- counter += 1;
- }
- counter = 0; // reset counter back to 0 so we can reuse.
- //goes through the paragraphs and outputs them (placing the current debt right after the first paragraph.)
- for (Element e : paragraphs) {
- System.out.printf(e.text());
- if(counter == 0){
- System.out.printf(" " + currentDebt + "\n");
- counter += 1;
- }
- }
- } catch (IOException e) {
- System.out.println("Error exception");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement