Maven
Şöyle yaparız
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.14.3</version> </dependency>
Jsoup Sınıfı
get metodu
Document döndürür
Örnek
Şöyle yaparız
String url = "https://www.cnn.com/";Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0").get();//get the whole pageString html = doc.html();//get the links on the pageElements links = doc.select("a[href]");for (Element link : links) {String line = link.attr("href");System.out.println(line);}
post metodu
Örnek
Şöyle yaparız
String url = "https://httpbin.org/post";Document doc = Jsoup.connect(url).ignoreContentType(true).timeout(1000).data("custname", "google").data("custtel", "234").userAgent("Mozilla/5.0").post();String str = doc.html();System.out.println(html);
Document Sınıfı
getElementsByTag metodu
Örnek
Şöyle yaparız
String url = "https://www.istockphoto.com/stock-illustrations";Document document = Jsoup.connect(url).userAgent("Mozilla/5.0").get();Elements tag = document.getElementsByTag("img");for (Element link : tag) {String imgurl = link.attr("abs:src");...}
select metodu
Örnek
Şöyle yaparız
public Set<String> listLinks(String url, boolean includeMedia) throws IOException { Document doc = Jsoup.connect(url).get(); Elements links = doc.select("a[href]"); Elements imports = doc.select("link[href]"); Set<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); if(includeMedia) { Elements media = doc.select("[src]"); for (Element src : media) { result.add(src.absUrl("src")); //result.add(src.attr("abs:src")); } } for (Element link : imports) { result.add(link.absUrl("abs:href")); } for (Element link : links) { result.add(link.absUrl("abs:href")); } return result; }
text metodu
Html tag'lerini ayıklar. Şöyle yaparız.
String htmlString = "<div class=\"WordSection1\"> <p class=\"MsoNormal\">Hi<br>
<br> <br> <br> Data is written in this mail.<br> <br> <br>
<br> <o:p></o:p></p> </div>";
System.out.println(Jsoup.parse(htmlString).text());
Çıktı olarak şunu alırızHi Data is written in this mail.
Hiç yorum yok:
Yorum Gönder