Jsoup kütüphanesi internet üzerinden bir sayfayı parse etmeniz gerektiğinde işinize yarayacak bir kütüphanedir. HTML parser olarak görebilirsiniz.
Örnek bir proje ve jsoup kodlarına bakarak anlatacağımız bu yazıda ilk olarak jsoup’ta ki methodlarına ve örneklerine bakalım.
İlk olarak jsoup kütüphanesini kurmak için yapmanız gerekenler.
1-) pom.xml için:
<dependency>
<!– jsoup HTML parser library @ https://jsoup.org/ –>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
2-) https://jsoup.org/download jar’ı indirip library kısmına atabilirsiniz.
Şimdi netbeanste bir örnek proje yapmadan önce jsoup’u tanıyalım. Herşeyi hazır kullanabiliriz.
Aşağıda bir çok örnek ekledim. En önemlisi ise bu örnekleri sizin yapmanızı istemem. Try ve catch kullancaksınız. Android’te kullanmak isteyenler AsynTask yapısı ile başlayabilirsiniz. Ama RXJAVA öğrenmenizi tavsiye ediyorum. (Bende yeni başladım. Yakın zamanda yazısını ekleyeceğim.)
ilk başta bir url’e bağlantı için yapmamız gereken:
|
1 |
Document doc = Jsoup.connect("https://www.umiitkose.com").get(); |
bir sayfanın title bilgisini almak için:
|
1 |
String title = doc.title(); |
html’deki bütün linkleri almak için:
|
1 2 3 4 5 6 |
Document doc = Jsoup.connect("https://www.umiitkose.com").get(); Elements links = doc.select("a[href]"); for (Element link : links) { System.out.println("\nlink : " + link.attr("href")); System.out.println("text : " + link.text()); } |
importlar için:
|
1 2 3 4 |
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; |
url’in meta bilgilerini almak için:
|
1 2 3 4 5 |
Document doc = Jsoup.connect("https://www.umiitkose.com").get(); String keywords = doc.select("meta[name=keywords]").first().attr("content"); System.out.println("Meta keyword : " + keywords); String description = doc.select("meta[name=description]").get(0).attr("content"); System.out.println("Meta description : " + description); |
url’den bütün resimleri almak için:
|
1 2 3 4 5 6 7 8 |
Document doc = Jsoup.connect("https://www.umiitkose.com").get(); Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]"); for (Element image : images) { System.out.println("src : " + image.attr("src")); System.out.println("height : " + image.attr("height")); System.out.println("width : " + image.attr("width")); System.out.println("alt : " + image.attr("alt")); } |
bir sayfanın title ve body’si için,
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void main(String[] args) { String htmlString = "<html><head><title>My title</title></head>" + "<body>Body content</body></html>"; Document doc = Jsoup.parse(htmlString); String title = doc.title(); String body = doc.body().text(); System.out.printf("Title: %s%n", title); System.out.printf("Body: %s", body); } |
Bir html file’i gösterip ondan veri almak için,
html file:
|
1 2 3 4 5 6 7 8 9 10 11 |
<!-- HTML file --> <!DOCTYPE html> <html> <head> <title> Hi Title ! </title> <meta charset="UTF-8"> </head> <body> <div id="mydiv">Hi Div !</div> </body> </html> |
kod:
|
1 2 3 4 5 6 7 8 9 |
public static void main(String[] args) throws IOException { String fileName = "path/to/file.html"; Document doc = Jsoup.parse(new File(fileName), "utf-8"); Element divTag = doc.getElementById("mydiv"); System.out.println(divTag.text()); } |
html kaynağını almak için:
|
1 2 3 4 5 6 7 8 |
public static void main(String[] args) throws IOException { String webPage = "https://www.umiitkose.com"; String html = Jsoup.connect(webPage).get().html(); System.out.println(html); } |
Bağlantının header, cookie vs özelleştirme ihtiyacnız olduğunda;
|
1 2 3 4 5 6 7 8 |
Connection connection = Jsoup.connect(blogUrl); connection.userAgent("Mozilla"); connection.timeout(5000); connection.cookie("cookiename", "val234"); connection.cookie("cookiename", "val234"); connection.referrer("https://google.com"); connection.header("headersecurity", "xyz123"); Document docCustomConn = connection.get(); |
Ek olarak html taglardan verileri alma kısmı,
|
1 2 3 4 5 6 |
Elements links = doc.select("a"); Elements sections = doc.select("section"); Elements logo = doc.select(".spring-logo--container"); Elements pagination = doc.select("#pagination_control"); Elements divsDescendant = doc.select("header div"); Elements divsDirect = doc.select("header > div"); |
id ve class’ı almak için,
|
1 2 |
Element pag = doc.getElementById("pagination_control"); Elements desktopOnly = doc.getElementsByClass("desktopOnly"); |
html tag’ler arasında gezinme,
|
1 2 3 4 5 6 7 |
Element firstSection = sections.first(); Element lastSection = sections.last(); Element secondSection = sections.get(2); Elements allParents = firstSection.parents(); Element parent = firstSection.parent(); Elements children = firstSection.children(); Elements siblings = firstSection.siblingElements(); |
text’e ulaşmak için:
|
1 2 3 4 5 6 7 |
Element firstArticle = doc.select("article").first(); Element timeElement = firstArticle.select("time").first(); String dateTimeOfFirstArticle = timeElement.attr("datetime"); Element sectionDiv = firstArticle.select("section div").first(); String sectionDivText = sectionDiv.text(); String articleHtml = firstArticle.html(); String outerHtml = firstArticle.outerHtml(); |
Örnek bir gazete manşetlerinden gazete, resim ve gazetenin manşetini alma uygulaması:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
Gazete.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jsoupgazetemansetoku; /** * Created by umiitkose on 23.07.2017. */ public class Gazete { private String gazeteIsim; private String gazeteResim; private String gazeteManset; public Gazete() { } public Gazete(String gazeteIsim, String gazeteResim, String gazeteManset) { this.gazeteIsim = gazeteIsim; this.gazeteResim = gazeteResim; this.gazeteManset = gazeteManset; } public String getGazeteIsim() { return gazeteIsim; } public void setGazeteIsim(String gazeteIsim) { this.gazeteIsim = gazeteIsim; } public String getGazeteResim() { return gazeteResim; } public void setGazeteResim(String gazeteResim) { this.gazeteResim = gazeteResim; } public String getGazeteManset() { return gazeteManset; } public void setGazeteManset(String gazeteManset) { this.gazeteManset = gazeteManset; } } |
JsoupGazeteOku.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jsoupgazetemansetoku; import java.util.ArrayList; import java.util.List; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; /** * * @author umiitkose */ public class JsoupGazeteMansetOku { /** * @param args the command line arguments */ public static void main(String[] args) { int sayac=1; List<Gazete> gazeteList=new ArrayList<>(); try { Document gazeteOku = Jsoup.connect("https://www.gazeteoku.com/gazete-mansetleri.html").get(); // System.out.println("html Data : "+gazeteOku.html().toString()); Elements gazeteler1 = gazeteOku.select(".list-papers"); Elements gazeteler=gazeteler1.select("li[class=col-xs-12 col-sm-6 col-md-4 col-lg-4]"); System.out.println("gazeteler Data : " + gazeteler.size()); for (Element element : gazeteler) { Gazete gazete=new Gazete(); gazete.setGazeteIsim(element.getElementsByClass("bar").text()); String tempText= element.select("img").first().toString(); tempText=tempText.replace("<img src=\"", ""); tempText=tempText.substring(0, tempText.indexOf("\"")); gazete.setGazeteResim(tempText); gazete.setGazeteManset(element.select("p").text()); gazeteList.add(gazete); } for(Gazete gazete : gazeteList){ System.out.println("------Gazete " + sayac+ " --------------" ); System.out.println("Name : " +gazete.getGazeteIsim()); System.out.println("İmage : "+gazete.getGazeteResim()); System.out.println("Manşet :"+gazete.getGazeteManset()); sayac++; System.out.println("-------------------------------------"); } }catch (HttpStatusException ex){} } } |
Çıktı:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
run: gazeteler Data : 38 ------Gazete 1 -------------- Name : Hürriyet İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83278.jpg Manşet :9 günde 2.tufan ------------------------------------- ------Gazete 2 -------------- Name : Sabah İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83271.jpg Manşet :Ekonomide iyi yoldayız ------------------------------------- ------Gazete 3 -------------- Name : Sözcü İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83283.jpg Manşet :MHP'li yöneticiden Merve Kavakçı tweeti ------------------------------------- ------Gazete 4 -------------- Name : Milliyet İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83267.jpg Manşet :Rüzgara hücum ------------------------------------- ------Gazete 5 -------------- Name : Habertürk İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83284.jpg Manşet :İkinci uyarı ------------------------------------- ------Gazete 6 -------------- Name : Posta İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83281.jpg Manşet :20 dakikada felaket ------------------------------------- ------Gazete 7 -------------- Name : Star İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83273.jpg Manşet :Almanlar Merkel'i dinlmedi ------------------------------------- ------Gazete 8 -------------- Name : Karar İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83276.jpg Manşet :İnterpol krizini bitiren görüntü ------------------------------------- ------Gazete 9 -------------- Name : Yeni Şafak İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83282.jpg Manşet :Bilginin merkezi İstanbul ------------------------------------- ------Gazete 10 -------------- Name : YeniBirlik İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83264.jpg Manşet :'Yorulan çekilsin' ------------------------------------- ------Gazete 11 -------------- Name : Akşam İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83268.jpg Manşet :Yorulan varsa kenara çekilsin ------------------------------------- ------Gazete 12 -------------- Name : Güneş İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83269.jpg Manşet :Tükenişin fotoğrafı ------------------------------------- ------Gazete 13 -------------- Name : Türkiye Gazetesi İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83257.jpg Manşet :Dolu,fırtına,sel vurdu geçti ------------------------------------- ------Gazete 14 -------------- Name : Cumhuriyet İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83277.jpg Manşet :Özgürlük ------------------------------------- ------Gazete 15 -------------- Name : Yeni Akit İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83274.jpg Manşet :O kafanın derdi İslam ------------------------------------- ------Gazete 16 -------------- Name : Milli Gazete İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83255.jpg Manşet :Kudüs Türkiye'yi bekliyor ------------------------------------- ------Gazete 17 -------------- Name : Vatan İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83256.jpg Manşet :Ağlatan mektup ------------------------------------- ------Gazete 18 -------------- Name : Evrensel İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83253.jpg Manşet :KHK'ler iptal edilsin iş güvencesi sağlansın ------------------------------------- ------Gazete 19 -------------- Name : Dünya İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83252.jpg Manşet :Başbakan'dan Alman devlere güven mesajı ------------------------------------- ------Gazete 20 -------------- Name : Takvim İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83272.jpg Manşet :Türk rüzgarı ------------------------------------- ------Gazete 21 -------------- Name : Yeniçağ İmage : https://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83260.jpg Manşet :Meclis'te tek başına ------------------------------------- ------Gazete 22 -------------- Name : Ortadoğu İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83259.jpg Manşet :İsrail küstahlığı ------------------------------------- ------Gazete 23 -------------- Name : Milat İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83266.jpg Manşet :Çıldırtan terör ------------------------------------- ------Gazete 24 -------------- Name : Fanatik İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83279.jpg Manşet :Haydi Fenerliler eller havaya ------------------------------------- ------Gazete 25 -------------- Name : Fotomaç İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83270.jpg Manşet :Alper Tunga ------------------------------------- ------Gazete 26 -------------- Name : Yeni Asya İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83261.jpg Manşet :Bu duyarlılık devam etsin ------------------------------------- ------Gazete 27 -------------- Name : Yeni Mesaj İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83262.jpg Manşet :Barzani çıkmazda ------------------------------------- ------Gazete 28 -------------- Name : Anayurt İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83263.jpg Manşet :İkiyüzlü nato ------------------------------------- ------Gazete 29 -------------- Name : Diriliş Postası İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83288.jpg Manşet :Hainlerin Meclis'teki timi! ------------------------------------- ------Gazete 30 -------------- Name : Korkusuz İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83289.jpg Manşet :Kurşun gibi sözler ------------------------------------- ------Gazete 31 -------------- Name : Yeni Söz Gazetesi İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83265.jpg Manşet :Ahmet suçlu da Bülent ve İbrahim masum mu? ------------------------------------- ------Gazete 32 -------------- Name : Yurt Gazetesi İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83285.jpg Manşet :Noter onaylı torpil ------------------------------------- ------Gazete 33 -------------- Name : Daily Sabah İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83275.jpg Manşet : ------------------------------------- ------Gazete 34 -------------- Name : Hürriyet Daily News İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83258.jpg Manşet : ------------------------------------- ------Gazete 35 -------------- Name : AMK İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83287.jpg Manşet :Kocaman 1-2 avantaj ------------------------------------- ------Gazete 36 -------------- Name : Aydınlık Gazetesi İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83286.jpg Manşet :İç çatışma pususunda ------------------------------------- ------Gazete 37 -------------- Name : Hürses İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83254.jpg Manşet :Yıldırım: Sizi bu ülkenin şirketi olarak görüyoruz ------------------------------------- ------Gazete 38 -------------- Name : İstiklal Gazetesi İmage : http://i.gazeteoku.com/4/200/300//files/newspaper/2017/07/28/83280.jpg Manşet :Yorulan varsa kenara çekilsin ------------------------------------- BUILD SUCCESSFUL (total time: 0 seconds) |
4 thoughts on “Java Jsoup Nedir ve Kullanımı”
Ahmet M
(1 Ağustos 2017 - 03:46)Yazın çok güzel ellerine sağlık. Jsoup’u en çok clean özelliği için kullanıyorum. Belki o da eklenebilir yazıya 🙂
umiitkose
(1 Ağustos 2017 - 09:31)Merhaba Ahmet Bey,
O özelliği eklemeyi unutmuşum gün içerisinde güncelleme yaparak yazıya ekleyeceğim 🙂
Sedat
(30 Ekim 2019 - 22:45)Hocam jsoup ile bütün sitelerden çekebiliyomuyuz telif gibi bişi yeniyomu
umiitkose
(4 Kasım 2019 - 10:29)Tüm sitelerden veri çekebilirsin. Telif yenmez ama verileri çektiğin kurum bu konuyla ilgili yaptırımda bulunabilir.