使用Jsoup抓取車標網各種型別相應車的資訊
【谷歌翻譯,參考可以看官方原文】
jsoup:Java的HTML解析器
jsoup是與現實世界的HTML工作的Java庫。它提供了用於提取和操作資料,使用最好的DOM,
CSS和jquery的方法很像,而且的API很方便。
jsoup實現了WHATWG的HTML5規範,並解析HTML到同一個DOM現代瀏覽器做。
刮從一個URL,檔案或字串解析HTML
發現並提取資料,使用DOM遍歷或CSS選擇器 操縱HTML元素,屬性和文字
對一個安全白名單幹淨的使用者提交的內容,以防止XSS攻擊 輸出HTML整潔
jsoup是專門用來對付HTML各品種在野外發現的;從原始和驗證,無效標籤湯; jsoup將建立一個明智的解析樹。
//Document doc=Jsoup.parse(new URL(requestURL), 3000);
// Document doc=Jsoup.connect(requestURL).timeout(5000).get();
//Document doc = Jsoup.connect(requestURL).timeout(3000).userAgent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0")).get();
Document doc = Jsoup.connect(requestURL).timeout(3000).cookie("auth", "token").userAgent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0").get();
Jsoup.connect( homepage).userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)").referrer("www.google.com").get()
例
獲取維基百科網頁,它解析為一個DOM,並從在新聞欄目中選擇頭條新聞為元素(線上樣品)的列表:
Document doc =Jsoup.connect("http://en.wikipedia.org/").get();
Elements newsHeadlines = doc.select("#mp-itn b a");//獲取id為mp-itn下面b標籤的a標籤的元素
開源 jsoup是在寬鬆的MIT許可證分發的一個開源專案。原始碼可以在GitHub上。 入門
下載jsoup罐子(版本1.8.1) 閱讀菜譜介紹 享受! 開發和支援 如果您對如何使用jsoup,
或者有想法將來的發展有任何疑問,請通過郵件列表聯絡。
如果您發現任何問題,請檢查重複之後提交的bug。 狀態 jsoup是一般的釋出。
你抓取得太狠了吧,速度快的話,對方網站伺服器會不定時有一會無響應,處理如下:
1.對方網站有多個IP的時候,自己寫個分發類,輪流去每個ip取。
2.對於1個IP的時候,遇到這種情況,當前執行緒就自動暫停幾秒鐘,然後再重試,自動馬上重試的話,也有問題。
建議jsoup和httpclient一起用,httpclient去抓取資訊,jsoup做分析。上面2種處理,用httpclient都好解決的,jsoup的特長在於分析,抓取是httpclient的特長。
connectTimeout和soTimeout一般都設定3秒就好了,抓取麼,用單例就好,多執行緒的話,更容易遇到timeout。
package ivyy.taobao.com.jsoup;
import ivyy.taobao.com.entity.CheBiao;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
*@Date:2015-1-6
*@Author:liangjilong
*@Email:[email protected]
*@Version:1.0
*@Description:使用Jsoup抓取車標網各種相應車的資訊
*/
public class JsoupCar {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
/**
* 國產,日本,德國,法國,義大利,英國,美國,韓國,其他
*/
String [] countryNames={"guochan","riben","deguo","faguo","yidali","yingguo","meiguo","hanguo","qita"};
//遍歷獲取太多資訊估計會超時連線,可以單獨一個一個的去設值抓取如
//List<CheBiao> listsChe=getCheBiaoInfoByHtml("guochan");
for(String countryName:countryNames){
//System.out.println(countryName);
List<CheBiao> listsChe=getCheBiaoInfoByHtml(countryName);//
int count=1;
for (Iterator iterator = listsChe.iterator(); iterator.hasNext();) {
CheBiao cheBiao = (CheBiao) iterator.next();
System.out.println("第"+count+"-------"+cheBiao.getDetailText());
//System.out.println("第"+count+"-------"+cheBiao.getConcise());
count++;
}
}
}
/***
* 根據相應的國家品牌的名稱去獲取車的資訊
* @param countryName
* @return
*/
public static List<CheBiao> getCheBiaoInfoByHtml(String countryName)throws Exception{
List<CheBiao> listsChe=new ArrayList<CheBiao>();
String url=getUrl(countryName);
Document doc=Jsoup.parse(new URL(url), 3000);//方法一
//Document doc=Jsoup.connect(url).get();//方法二
if(doc!=null){
//處理從頁面的class=expPicA樣式下面的li標籤
Elements liEls=doc.getElementsByAttributeValue("class", "expPicA").select("li");
for(Element li:liEls){
CheBiao che=new CheBiao();
//從i標籤的class=iTit的a標籤拿出相應的資訊內容出來
String carName=li.select("i[class=iTit]").select("a").text().trim();//獲取汽車名稱
String imgSmallSrc=li.select("i[class=iTxt]").select("img").attr("src");//獲取汽車圖片路徑
String concise=li.select("i[class=iDes]").text().trim();//簡要
String detailUrl=li.select("i[class=iPic]").select("a").attr("href");//獲取汽車詳情的html頁面連線
Document descDoc=Jsoup.parse(new URL(detailUrl), 3000);//方法一
String imgBigSrc="",detailText="";
if(descDoc!=null){
Element article=descDoc.select("div[class=article]").get(0);
detailText=article.html();//獲取詳情資訊
imgBigSrc=article.select("img").attr("src");//獲取大圖片
}
che.setCarName(carName);
che.setConcise(concise);
che.setImgSmallSrc(imgSmallSrc);
che.setDetailUrl(detailUrl);
che.setImgBigSrc(imgBigSrc);
che.setDetailText(detailText);
listsChe.add(che);
}
return listsChe;
}else{
//html="Network Connect Timeout";
}
return null;
}
/***
* 根據相應的國家品牌的名稱請求相應的連線
* @param countryName
* @return
*/
public static String getUrl(String countryName){
return "http://www.pcauto.com.cn/zt/chebiao/"+countryName;
}
}
package ivyy.taobao.com.entity;
import java.io.Serializable;
/**
*@Date:2015-1-6
*@Author:liangjilong
*@Email:[email protected]
*@Version:1.0
*@Description:實體類
*/
@SuppressWarnings("all")
public class CheBiao implements Serializable{
private Integer id;//id標識
private String carName;//汽車名稱
private String concise;//簡要說明
private String imgSmallSrc;//汽車小圖片路徑
private String imgBigSrc;//汽車大圖片路徑
private String detailUrl;//汽車詳情頁面路徑
private String detailText;//汽車詳情描述
/******************get/set*****************************/
public String getDetailText() {
return detailText;
}
public void setDetailText(String detailText) {
this.detailText = detailText;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public String getConcise() {
return concise;
}
public void setConcise(String concise) {
this.concise = concise;
}
public String getImgSmallSrc() {
return imgSmallSrc;
}
public void setImgSmallSrc(String imgSmallSrc) {
this.imgSmallSrc = imgSmallSrc;
}
public String getImgBigSrc() {
return imgBigSrc;
}
public void setImgBigSrc(String imgBigSrc) {
this.imgBigSrc = imgBigSrc;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
}