jsoup是一款Java的HTML解析器俊鱼,主要用來(lái)對(duì)HTML解析蛹磺。官網(wǎng) 中文文檔
在爬蟲的時(shí)候,當(dāng)我們用HttpClient之類的框架瞧哟,獲取到網(wǎng)頁(yè)源碼之后混巧,需要從網(wǎng)頁(yè)源碼中取出我們想要的內(nèi)容,就可以使用jsoup這類HTML解析器了勤揩∵值常可以非常輕松的實(shí)現(xiàn)。雖然jsoup也支持從某個(gè)地址直接去爬取網(wǎng)頁(yè)源碼陨亡,但是只支持HTTP傍衡,HTTPS協(xié)議深员,支持不夠豐富。所以蛙埂,主要還是用來(lái)對(duì)HTML進(jìn)行解析倦畅。
◆其中,要被解析的HTML可以是一個(gè)HTML的字符串绣的,可以是一個(gè)URL叠赐,可以是一個(gè)文件。
org.jsoup.Jsoup把輸入的HTML轉(zhuǎn)換成一個(gè)org.jsoup.nodes.Document對(duì)象屡江,然后從Document對(duì)象中取出想要的元素燎悍。
org.jsoup.nodes.Document繼承了org.jsoup.nodes.Element,Element又繼承了org.jsoup.nodes.Node類盼理。里面提供了豐富的方法來(lái)獲取HTML的元素谈山。
◇從URL獲取HTML來(lái)解析
Document doc = Jsoup.connect("http://www.baidu.com/").get();
String title = doc.title();
其中Jsoup.connect("xxx")方法返回一個(gè)org.jsoup.Connection對(duì)象。
在Connection對(duì)象中宏怔,我們可以執(zhí)行g(shù)et或者post來(lái)執(zhí)行請(qǐng)求奏路。但是在執(zhí)行請(qǐng)求之前,
我們可以使用Connection對(duì)象來(lái)設(shè)置一些請(qǐng)求信息臊诊。比如:頭信息鸽粉,cookie,請(qǐng)求等待時(shí)間抓艳,代理等等來(lái)模擬瀏覽器的行為触机。
Document doc = Jsoup.connect("http://example.com")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
◆獲得Document對(duì)象后,接下來(lái)就是解析Document對(duì)象玷或,并從中獲取我們想要的元素了儡首。
Document中提供了豐富的方法來(lái)獲取指定元素。
◇使用DOM的方式來(lái)取得
getElementById(String id):通過(guò)id來(lái)獲取
getElementsByTag(String tagName):通過(guò)標(biāo)簽名字來(lái)獲取
getElementsByClass(String className):通過(guò)類名來(lái)獲取
getElementsByAttribute(String key):通過(guò)屬性名字來(lái)獲取
getElementsByAttributeValue(String key, String value):通過(guò)指定的屬性名字偏友,屬性值來(lái)獲取
getAllElements():獲取所有元素
◇通過(guò)類似于css或jQuery的選擇器來(lái)查找元素
使用的是Element類的下記方法:
public Elements select(String cssQuery)
通過(guò)傳入一個(gè)類似于CSS或jQuery的選擇器字符串蔬胯,來(lái)查找指定元素。
例子:
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Elements links = doc.select("a[href]"); //帶有href屬性的a元素
Elements pngs = doc.select("img[src$=.png]");
//擴(kuò)展名為.png的圖片
Element masthead = doc.select("div.masthead").first();
//class等于masthead的div標(biāo)簽
Elements resultLinks = doc.select("h3.r > a"); //在h3元素之后的a元素
選擇器的更多語(yǔ)法(可以在org.jsoup.select.Selector中查看到更多關(guān)于選擇器的語(yǔ)法):
tagname: 通過(guò)標(biāo)簽查找元素位他,比如:a
ns|tag: 通過(guò)標(biāo)簽在命名空間查找元素氛濒,比如:可以用 fb|name 語(yǔ)法來(lái)查找 <fb:name> 元素
#id: 通過(guò)ID查找元素,比如:#logo
.class: 通過(guò)class名稱查找元素鹅髓,比如:.masthead
[attribute]: 利用屬性查找元素舞竿,比如:[href]
[^attr]: 利用屬性名前綴來(lái)查找元素,比如:可以用[^data-] 來(lái)查找?guī)в蠬TML5 Dataset屬性的元素
[attr=value]: 利用屬性值來(lái)查找元素窿冯,比如:[width=500]
[attr^=value], [attr$=value], [attr=value]: 利用匹配屬性值開頭骗奖、結(jié)尾或包含屬性值來(lái)查找元素,比如:[href=/path/]
[attr~=regex]: 利用屬性值匹配正則表達(dá)式來(lái)查找元素,比如: img[src~=(?i).(png|jpe?g)]
*: 這個(gè)符號(hào)將匹配所有元素
Selector選擇器組合使用
el#id: 元素+ID重归,比如: div#logo
el.class: 元素+class米愿,比如: div.masthead
el[attr]: 元素+class,比如: a[href]
任意組合鼻吮,比如:a[href].highlight
ancestor child: 查找某個(gè)元素下子元素育苟,比如:可以用.body p 查找在"body"元素下的所有 p元素
parent > child: 查找某個(gè)父元素下的直接子元素,比如:可以用div.content > p 查找 p 元素椎木,也可以用body > * 查找body標(biāo)簽下所有直接子元素
siblingA + siblingB: 查找在A元素之前第一個(gè)同級(jí)元素B违柏,比如:div.head + div
siblingA ~ siblingX: 查找A元素之前的同級(jí)X元素,比如:h1 ~ p
el, el, el:多個(gè)選擇器組合香椎,查找匹配任一選擇器的唯一元素漱竖,例如:div.masthead, div.logo
偽選擇器selectors
:lt(n): 查找哪些元素的同級(jí)索引值(它的位置在DOM樹中是相對(duì)于它的父節(jié)點(diǎn))小于n,比如:td:lt(3) 表示小于三列的元素
:gt(n):查找哪些元素的同級(jí)索引值大于n畜伐,比如: div p:gt(2)表示哪些div中有包含2個(gè)以上的p元素
:eq(n): 查找哪些元素的同級(jí)索引值與n相等馍惹,比如:form input:eq(1)表示包含一個(gè)input標(biāo)簽的Form元素
:has(seletor): 查找匹配選擇器包含元素的元素,比如:div:has(p)表示哪些div包含了p元素
:not(selector): 查找與選擇器不匹配的元素玛界,比如: div:not(.logo) 表示不包含 class="logo" 元素的所有 div 列表
:contains(text): 查找包含給定文本的元素万矾,搜索不區(qū)分大不寫,比如: p:contains(jsoup)
:containsOwn(text): 查找直接包含給定文本的元素
:matches(regex): 查找哪些元素的文本匹配指定的正則表達(dá)式慎框,比如:div:matches((?i)login)
:matchesOwn(regex): 查找自身包含文本匹配指定正則表達(dá)式的元素
注意 ×急贰:上述偽選擇器索引是從0開始的,也就是說(shuō)第一個(gè)元素索引值為0笨枯,第二個(gè)元素index為1等
◆通過(guò)上面的選擇器薪丁,我們可以取得一個(gè)Elements對(duì)象,它繼承了ArrayList對(duì)象馅精,里面放的全是Element對(duì)象严嗜。
接下來(lái)我們要做的就是從Element對(duì)象中,取出我們真正需要的內(nèi)容硫嘶。
通常有下面幾種方法:
◇Element.text()
這個(gè)方法用來(lái)取得一個(gè)元素中的文本阻问。
◇Element.html()或Node.outerHtml()
這個(gè)方法用來(lái)取得一個(gè)元素中的html內(nèi)容
◇Node.attr(String key)
獲得一個(gè)屬性的值梧税,例如取得超鏈接<a href="">中href的值
綜合實(shí)例:采集開源中國(guó)項(xiàng)目信息
package com.company;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) throws IOException {
// write your code here
Set<String> setUrls = new HashSet<>();
for(int i = 1; i <= 5; i++)
{
String strUrl = "https://www.oschina.net/project/list?company=0&sort=score&lang=0&recommend=false&p="+i;
setUrls.add(strUrl);
}
Set<String> setProjUrls = new HashSet<>();
for(String stringUrl : setUrls)
{
Document document = Jsoup.connect(stringUrl)
.userAgent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0")
.get();
Elements elements1 = document.select(".news-list");
Elements elements = elements1.select("div.box");
for(Element element : elements)
{
Elements eleUrl = element.select("div.box-aw a");
String strPrjUrl = eleUrl.attr("href");
setProjUrls.add(strPrjUrl);
// System.out.println(strPrjUrl);
Elements eleTitle = eleUrl.select(".title");
String strTitle = eleTitle.text();
// System.out.println(strTitle);
Elements eleSummary = eleUrl.select(".summary");
String strSummary = eleSummary.text();
// System.out.println(strSummary);
}
}
for(String stringUrl : setProjUrls)
{
Document document = Jsoup.connect(stringUrl)
.userAgent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0")
.get();
Elements elements = document.select("div.box-aw a h1");
String strTitle = elements.text();
System.out.println("標(biāo)題:" + strTitle);
Elements elementsSection = document.select("section.list");
int nSize = elementsSection.get(0).children().size();
if(nSize == 0)
continue;
Element elementProtocol = elementsSection.get(0).child(0);
Elements elesPro = elementProtocol.select("span");
String strPro = elesPro.text();
System.out.println("開源協(xié)議:" + strPro);
nSize--;
if(nSize == 0)
continue;
Element elementLan = elementsSection.get(0).child(1);
Elements elesLan = elementLan.select("span").get(0).children();
StringBuilder strlan = new StringBuilder();
for(Element ele : elesLan)
{
String strLanTemp = ele.text();
if(strLanTemp.indexOf("查看源碼")>=0)
break;
strlan.append(strLanTemp+",");
}
if(elesLan.size()>0)
{
String strLanguage = strlan.toString().substring(0,strlan.length()-1);
System.out.println("開發(fā)語(yǔ)言:" + strLanguage);
}
nSize--;
if(nSize == 0)
continue;
Element elementOS = elementsSection.get(0).child(2);
Elements elesOS = elementOS.select("span");
String strOS = elesOS.text();
System.out.println("操作系統(tǒng):" + strOS);
nSize--;
if(nSize == 0)
continue;
Element elementAuthor = elementsSection.get(0).child(3);
Elements elesAuthor = elementAuthor.select("a.link");
String strAuthor= elesAuthor.text();
System.out.println("軟件作者沦疾;" + strAuthor);
System.out.println("---------------------");
}
}
}
爬取騰訊首頁(yè)全部圖片
package com.company;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class meizi {
/**
* 下載圖片到指定目錄
*
* @param filePath 文件路徑
* @param imgUrl 圖片URL
*/
public static void downImages(String filePath, String imgUrl) {
// 若指定文件夾沒(méi)有,則先創(chuàng)建
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
// 截取圖片文件名
String fileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length());
try {
// 文件名里面可能有中文或者空格第队,所以這里要進(jìn)行處理哮塞。但空格又會(huì)被URLEncoder轉(zhuǎn)義為加號(hào)
String urlTail = URLEncoder.encode(fileName, "UTF-8");
// 因此要將加號(hào)轉(zhuǎn)化為UTF-8格式的%20
imgUrl = imgUrl.substring(0, imgUrl.lastIndexOf('/') + 1) + urlTail.replaceAll("\\+", "\\%20");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 寫出的路徑
File file = new File(filePath + File.separator + fileName);
try {
// 獲取圖片URL
URL url = new URL(imgUrl);
// 獲得連接
URLConnection connection = url.openConnection();
// 設(shè)置10秒的相應(yīng)時(shí)間
connection.setConnectTimeout(10 * 1000);
// 獲得輸入流
InputStream in = connection.getInputStream();
// 獲得輸出流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
// 構(gòu)建緩沖區(qū)
byte[] buf = new byte[1024];
int size;
// 寫入到文件
while (-1 != (size = in.read(buf))) {
out.write(buf, 0, size);
}
out.close();
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 利用Jsoup獲得連接
Connection connect = Jsoup.connect("http://www.qq.com");
try {
// 得到Document對(duì)象
Document document = connect.get();
// 查找所有img標(biāo)簽
Elements imgs = document.getElementsByTag("img");
System.out.println("共檢測(cè)到下列圖片URL:");
System.out.println("開始下載");
// 遍歷img標(biāo)簽并獲得src的屬性
for (Element element : imgs) {
//獲取每個(gè)img標(biāo)簽URL "abs:"表示絕對(duì)路徑
String imgSrc = element.attr("abs:src");
// 打印URL
System.out.println(imgSrc);
//下載圖片到本地
meizi.downImages("d:/img", imgSrc);
}
System.out.println("下載完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
解析json(悟空問(wèn)答網(wǎng)案例)
package com.company;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
/**
* Created by Administrator on 2018/8/8.
*/
public class hello {
public static void main(String[] args) throws IOException {
Connection.Response res = Jsoup.connect("https://www.wukong.com/wenda/web/nativefeed/brow/?concern_id=6300775428692904450&t=1533714730319&_signature=DKZ7mhAQV9JbkTPBachKdgyme4")
.header("Accept", "*/*")
.header("Accept-Encoding", "gzip, deflate")
.header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
.header("Content-Type", "application/json;charset=UTF-8")
.header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
.timeout(10000).ignoreContentType(true).execute();//.get();
String body = res.body();
System.out.println(body);
JSONObject jsonObject2 = JSON.parseObject(body);
JSONArray jsonArray = jsonObject2.getJSONArray("data");
//JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因?yàn)镴SONArray繼承了JSON,所以這樣也是可以的
//遍歷方式1
int size = jsonArray.size();
for (int i = 0; i < size; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
if(jsonObject.containsKey("question"))
{
JSONObject jsonObject3 = jsonObject.getJSONObject("question");
String qid = jsonObject3.getString("qid");
System.out.println(qid);
}
}
}
}
fastjson補(bǔ)充
json字符串-數(shù)組類型與JSONArray之間的轉(zhuǎn)換
/**
* json字符串-數(shù)組類型與JSONArray之間的轉(zhuǎn)換
*/
public static void testJSONStrToJSONArray(){
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
//JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因?yàn)镴SONArray繼承了JSON凳谦,所以這樣也是可以的
//遍歷方式1
int size = jsonArray.size();
for (int i = 0; i < size; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
//遍歷方式2
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
}
復(fù)雜json格式字符串與JSONObject之間的轉(zhuǎn)換
/**
* 復(fù)雜json格式字符串與JSONObject之間的轉(zhuǎn)換
*/
public static void testComplexJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因?yàn)镴SONObject繼承了JSON忆畅,所以這樣也是可以的
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
JSONObject course = jsonObject.getJSONObject("course");
JSONArray students = jsonObject.getJSONArray("students");
}
另一個(gè)實(shí)例(采集悟空問(wèn)答某個(gè)問(wèn)題的評(píng)論信息)
@Test
public void testWukongAnswer() throws IOException {
Document doc = Jsoup.connect("https://www.wukong.com/question/6586489850478723332/").get();
Elements elements = doc.select("div.answer-item");
for(Element element : elements)
{
Element elementImg = element.selectFirst(".answer-user-avatar img");
System.out.println(elementImg.attr("abs:src"));
Element elementName = element.selectFirst(".answer-user-name");
System.out.println(elementName.text());
Element elementDate = element.selectFirst(".answer-user-tag");
System.out.println(elementDate.text());
Element elementContent = element.selectFirst("div.answer-text-full");
System.out.println(elementContent.text());
}
}
練習(xí)
爬取獵聘網(wǎng) java關(guān)鍵字 企業(yè)招聘信息(包含詳情)
https://www.liepin.com/