Guest User

Untitled

a guest
Aug 11th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.URL;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import java.util.Scanner;
  7. import org.jsoup.Jsoup;
  8. import org.jsoup.nodes.Document;
  9. import org.jsoup.nodes.Element;
  10. import org.jsoup.select.Elements;
  11.  
  12. public class ShowLinksApp {
  13. public static void main(String[] args) throws IOException {
  14.  
  15. System.out.println("Type valid url e.g.\"http://economist.com\":");
  16.  
  17. Scanner sc = new Scanner(System.in);
  18. String url = sc.nextLine();
  19. sc.close();
  20.  
  21. System.out.printf("Processing %s...\n", url);
  22.  
  23. Map<String,Integer> linkMap = new HashMap<String,Integer>();
  24. Document doc = Jsoup.connect(url).get();
  25. Elements links = doc.select("a");
  26.  
  27. System.out.printf("\nLinks: (%d)\n", links.size());
  28.  
  29. for (Element link : links) {
  30.  
  31. String absURL = link.attr("abs:href");
  32. URL u = new URL(absURL);
  33. String hostURL = u.getHost();
  34.  
  35.  
  36. if (linkMap.containsKey(hostURL)) {
  37. linkMap.put(hostURL, linkMap.get(hostURL) + 1);
  38. } else {
  39. linkMap.put(hostURL, 1);
  40. }
  41.  
  42. }
  43. for (Entry<String, Integer> e : linkMap.entrySet()) {
  44. System.out.println(e.getKey() + " - " + e.getValue());
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment