jsoup 使用教程
jsoup是一款Java的html解析工具,主要是對html和xml文件進行解析
在寫爬蟲的時候,當我們用HttpClient之類的框架,得到目標網(wǎng)頁的源碼后,需要從網(wǎng)頁源碼中取得我們想要的內(nèi)容。就可以使用jsoup輕松獲取想要的內(nèi)容。
jsoup的中文開發(fā)文檔
獲取jsoup的maven方式
<dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version> </dependency>從URL獲取HTML來解析
// 方式一 Document document = Jsoup.connect("http://www.baidu.com/").get(); // 方式二 Document parse = Jsoup.parse(new URL("http://www.baidu.com/"), 1000 * 10);其中Jsoup.connect(“xxx”)方法返回一個org.jsoup.Connection對象。
在Connection對象中,我們可以執(zhí)行get和post來執(zhí)行請求。但在執(zhí)行請求之前,我們可以使用Connection對象來設置一些請求信息。比如:頭信息,cookie,請求等待時間,代理等等模擬瀏覽器行為。
從文件中獲取html來解析
Document path = Jsoup.parse(new File("path"), "utf-8");直接從字符串中獲得html來解析
Document text = Jsoup.parse("");通過Document獲取指定節(jié)點Element對象
通過方法來查找指定的節(jié)點Element
通過類似于css或者jQuery的選擇器來查找元素
public Elements select(String cssQuery) {return Selector.select(cssQuery, this);}File input = new File("/tmp/input.html");Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");//帶有href屬性的a元素Elements links = doc.select("a[href]");//擴展名為.png的圖片Elements pngs = doc.select("img[src$=.png]");//class等于masthead的div標簽Element masthead = doc.select("div.masthead").first();//在h3元素之后的a元素?? ?Elements resultLinks = doc.select("h3.r > a");?通過傳入一個類似于css或者jQuery的選擇器字符串,來查找指定元素
選擇器中更多語法可以在org.jsoup.select.Selector中查看到更多關于選擇器的語法
Selector中的語法簡介
tagName:通過標簽名查找元素,比如:a
ns|tag:通過標簽在命名空間查找元素,比如:可以用fb|name語法來查找<fb:name>元素
#id:通過ID來查找元素,比如#logo
.class:通過class名稱來查找元素 比如.master
[attribute]:利用屬性名稱來查找元素,如[href]
[^attribute]:利用屬性前綴來查找元素,比如:可以用[^data-]來查找?guī)в蠬TML中以data-開頭的屬性節(jié)點
[attr=value]:利用屬性值來查找元素,比如:[width=500]
[attr~=regx]:利用屬性值匹配正則表達式來查找元素,比如:img[src~=(?i).(png|jpe?g)]
*:這個符號將匹配所有元素
Selector選擇器組合使用
el#id:元素+ID,比如:div#logo
el.class:元素+class,比如:div.master
el[attr]:元素+attr,比如:a[href]? 任意組合,比如:a[href].heightlight
ancestor 空格 child:查找某個元素下的子元素,比如:可以用body p查找body下的所有p標簽
parent > child:查找某個父元素下的直接元素,比如:可以用div.content > p查找class是content的div中所有的p標簽
siblingA + siblingB:查找標簽A下的第一個B標簽,比如:div.head + div
siblingA ~ siblingB:查找siblingA同級第一個B標簽,比如:h1 ~ p
el,el,el:多個選擇器組合,查找匹配任一選擇器的唯一元素,例如:div.master,div.logo
Selector偽類選擇器
tag:lt(n):查找所有小于給定索引值的元素,從0開始計數(shù),比如tr:lt(2)
tag:gt(n):查找所有大于給定索引值的元素,從0開始計數(shù),比如tr:gt(2)
tag:eq(n):查找給定索引值的元素,比如tr:eq(2)
tag:has(seletor):查找匹配選擇器包含元素的元素,比如:div:has§表示哪些div包含p元素
tag:not(seletor):查找于選擇器不匹配的元素,比如:div:not(.logo)表示不包含class=”logo“元素的所有div列表
tag:contains(text):查找包含給定文本元素,搜索不區(qū)分大小寫,比如:p:contains(jsoup)
tag:containsOwn(text):查找直接包含指定文本的元素
tag:matches(regex):查找自身包含文本匹配指定的元素
通過上面的選擇器,我們可以取得一個Elements對象中,它繼承了ArrayList對象,里面放的全是Element對象。使用Element獲得想要的內(nèi)容
使用Element獲得想要的內(nèi)容
// 這個方法用來獲取一個元素中的文本 element.text()// 這個方法用來獲取一個元素中的html內(nèi)容 element.html()或Node.outerHtml()// 這個方法來取得一個元素的一個屬性值 Node.attr(String key) public class JsoupMain {public static void main(String[] args) {try {Document document = Jsoup.connect("http://www.baidu.com/").data("wd","我").userAgent("Mozilla").cookie("auth","token").timeout(3000).post();Elements elements = document.select("a");elements.forEach(element -> {System.out.println(element.text());});} catch (IOException e) {e.printStackTrace();}} }原文鏈接:https://blog.csdn.net/justLym/article/details/105715516
總結(jié)
以上是生活随笔為你收集整理的jsoup 使用教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度站长平台VIP俱乐部服务全面升级
- 下一篇: wxpython简介