日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jsoup 简单应用

發布時間:2025/3/12 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jsoup 简单应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JSOUP指的是前端爬蟲框架,對HTML網頁的一系列操作包括信息的獲取內容的修改等。

jsoup簡單應用

1.三種加載HTML的方法

@Testpublic void test1() throws IOException {//從URL加載HTMLDocument document = Jsoup.connect("http://www.guge.com").get();String title = document.title();//獲取html中的標題System.out.println("title :"+title);//從字符串加載HTMLString html = "<html><head><title>First parse</title></head>"+ "<body><p>Parsed HTML into a doc.</p></body></html>";Document doc = Jsoup.parse(html);title = doc.title();System.out.println("title :"+title);//從文件加載HTMLdoc = Jsoup.parse(new File("d:\\file\\html\\index.html"),"utf-8");title = doc.title();System.out.println("title :"+title);}

2.獲取html中的head,body,url等信息

@Testpublic void test2() throws IOException {Document document = Jsoup.connect("http://www.guge.com").get();String title = document.title();System.out.println("title :"+title);//獲取html中的headSystem.out.println(document.head());//獲取html中的body//System.out.println(document.body());//獲取HTML頁面中的所有鏈接Elements links = document.select("a[href]");for (Element link : links){System.out.println("link : "+ link.attr("href"));System.out.println("text :"+ link.text());}}

3.獲取URL的地址信息

@Testpublic void test3() throws IOException {Document document = Jsoup.connect("https://passport.lagou.com").get();System.out.println(document.head());//獲取URL的元信息String description = document.select("meta[name=description]").get(0).attr("content");System.out.println("Meta description : " + description);String keywords = document.select("meta[name=keywords]").first().attr("content");System.out.println("Meta keyword : " + keywords);}

4.根據class名稱獲取表單

@Testpublic void test4() throws IOException {Document document = Jsoup.connect("https://passport.lagou.com/login/login.html?signature=8ECBCDF2B86061432B425A0B94FC863B&service=https%253A%252F%252Fwww.lagou.com%252F&action=login&serviceId=lagou&ts=1547711303033").get();//獲取拉勾網登入頁面的body//System.out.println(document.body());//根據class名稱獲取表單Elements formElement = document.getElementsByClass("form_body");System.out.println(formElement.html());//獲取URL的元信息for (Element inputElement : formElement) {String placeholder = inputElement.getElementsByTag("input").attr("placeholder");System.out.println(placeholder);}}

5.提取并打印表單參數

@Testpublic void test5() throws IOException {Document document = Jsoup.parse(new File("d:\\file\\html\\index.html"),"utf-8");Element loginform = document.getElementById("registerform");Elements inputElements = loginform.getElementsByTag("input");for (Element inputElement : inputElements) {String key = inputElement.attr("name");String value = inputElement.attr("value");System.out.println("Param name: "+key+" -- Param value: "+value);}}

6.設置元素的html內容

@Testpublic void test6() throws IOException {Document document = Jsoup.parse(new File("d:\\file\\html\\index.html"),"utf-8");System.out.println(document.body());// <div id="div1"></div>System.out.println("----------------");Element div = document.select("div").first();div.html("<p>Hello</p>"); // <div id="div1"><p>Hello</p></div>div.prepend("<p>Fiest</p>"); //<div id="div1"><p>Fiest</p><p>Hello</p></div>div.append("<p>Last</p>"); //<div id="div1"><p>Fiest</p><p>Hello</p><p>Last</p></div>System.out.println(document.body());System.out.println("------------------");System.out.println(div.text());System.out.println("-------------------");//對元素包裹一個外部HTML內容div.wrap("<div id=\"div2\"></div>"); //<div id="div2"><div id="div1"><p>Fiest</p><p>Hello</p><p>Last</p></div>System.out.println(document.body());}

7.設置元素的文本內容

@Testpublic void test7() throws IOException {Document document = Jsoup.parse(new File("d:\\file\\html\\index.html"),"utf-8");System.out.println(document.body());// <div id="div1"></div>System.out.println("-------------------");Element div = document.select("div").first();div.text("7 > 8 "); // <div id="div1">7 &gt; 8 </div>div.prepend("Fiest "); //<div id="div1">Fiest 7 &gt; 8</div>div.append("Last "); //<div id="div1">Fiest 7 &gt; 8 Last</div>System.out.println(document.body());System.out.println("---------------");System.out.println(div.text());}

了解更多關注我喲!!!

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的jsoup 简单应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。