話不多說直接幹貨
目錄
爬取方法
main方法
爬取的網站源碼
篩選方法
實際資料篩選
源碼以及所用的包
結束
我用到的jar包為jsoup-1.11.3.rar(這個包我會再最後留下分享連結)
隻要是靜态網站我用的都是這個包進行爬取.
爬取方法
//爬取方法,傳入網站網網址,傳回爬取的所有源碼以String類型
public Document doc = null;
public String Sdoc = null;
public String Crawlers(String url) {
try {
doc = Jsoup.connect(url)
.header("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) "+
"Chrome/47.0.2526.106 BIDUBrowser/8.7 Safari/537.36")
.ignoreContentType(true).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将Document 轉為 String類型方法 Document.html();
Sdoc = doc.html();
return Sdoc;
}
這裡面.header()裡的參數為模拟浏覽器類型(Chrome:谷歌浏覽器,Mozilla:火狐等)
不用過多講解.
main方法
我就以我之前的部落格為目标進行爬取
https://blog.csdn.net/qq_41873771/article/details/84142691
//main方法運作
public class Totality {
public static void main(String[] args) {
Totality Totality = new Totality();
String url = "https://blog.csdn.net/qq_41873771/article/details/84142691";
String text = Totality.Crawlers(url);
System.out.println(text);
}
}
爬取的網站源碼
結果就是傳回網站的源碼,我們要爬取的資訊就在這些源碼中
使用ctrl+F進行全局搜尋:
article-title-box
hide-article-box
搜尋完你會發現,文章的主要内容就在這些關鍵詞的中間,是以我們要通過篩選的方法一點一點篩選出來.
篩選方法
其實篩選就好似剝洋蔥,一層一層.
我這裡主要用一下幾個方法
.indexOf();
.lastIndexOf();
.substring();
.replace();
接下來我一個一個講解這些方法怎麼用,如果隻是想看結果的可以跳過這裡.
.indexOf();
String text = "abcdefghidjk";
//我這裡寫了兩個b
int i = text.indexOf("d");
System.out.println(i);
//最後列印出字元串f的下标為3(下标都是從0開始數的)
傳回指定的字元串在因一個字元串中首次出現的位置,從前往後找
與.lastIndexOf();有些不同
String text = "abcdefghidjk";
//我這裡寫了兩個b
int i = text.lastIndexOf("d");
System.out.println(i);
//最後列印出字元串f的下标為9(下标都是從0開始數的)
傳回指定的字元串值最後出現的位置,從後往前找
這兩個方法用于定位想要擷取的值的位置
就像上面的兩個關鍵詞一樣,用來定位文章内容的位置,因為網站源碼東西太多,是以需要将有用的單獨拿出來.
所有就用到了.substring();方法
String text = "abcdefghidjk";
int j = text.indexOf("d");
int i = text.lastIndexOf("d");
String zhi = text.substring(j,i);
System.out.println(zhi);
//列印defghi
方法用于提取字元串中介于兩個指定下标之間的字元。
這裡就用到了之前擷取的兩個下标.
但有一點注意indexOf()方法擷取的下标是包含自己的
比如上面我擷取的是第一個b的下标,我截取是就會将b也截取在内.
是以我的習慣是在substring();中,将前面的下标減去自己的長度
String text = "abcdefghidjk";
int j = text.indexOf("d");
int i = text.lastIndexOf("d");
String zhi = text.substring(j-1,i);
System.out.println(zhi);
//列印efghi
我就會擷取中間的内容.
.replace();方法
方法用于在字元串中用一些字元替換另一些字元
就是将内容替換成字元串空或其他字元,這裡主要用于替換為字元串空,也就是删除.
String text = "abcdefghidjk";
text = text.replace("a", "");
text = text.replace("d", "");
text = text.replace("g", "");
System.out.println(text);
//列印bcefhijk
replace()方法将a.d.g字元替換為字元串空,就是删除.
用到的方法講完.
實際資料篩選
public void Filter_no1(String text) {
// 擷取所需要的主要部分
beginIndex = text.indexOf("<div class=\"article-title-box\">");
endIndex = text.indexOf("<div class=\"hide-article-box text-center\">");
text = text.substring(beginIndex, endIndex);
// 對主要部分進行篩選
// 擷取文章類型
beginIndex = text.lastIndexOf("left\">");
endIndex = text.indexOf("</span>");
String article_type = text.substring(beginIndex + 6, endIndex);
// 擷取标題
beginIndex = text.indexOf("title-article");
endIndex = text.indexOf("</h1>");
String title_article = text.substring(beginIndex + 15, endIndex);
// 擷取時間
endIndex = text.indexOf("time");
String time = text.substring(endIndex + 6);// sub方法一個值時從這個下标開始到String結尾
endIndex = time.indexOf("</span>");
time = time.substring(0, endIndex);// 下标從0開始
// 閱讀數
endIndex = text.indexOf("read-count");
String read_count = text.substring(endIndex + 12);// sub方法一個值時從這個下标開始到String結尾
endIndex = read_count.indexOf("</span>");
read_count = read_count.substring(0, endIndex);// 下标從0開始
// 使用者名
endIndex = text.indexOf("follow-nickName");
String Name = text.substring(endIndex);// sub方法一個值時從這個下标開始到String結尾
beginIndex = Name.indexOf(">");
endIndex = Name.indexOf("</a>");
Name = Name.substring(beginIndex + 1, endIndex);// 下标從0開始
// 個人分類
String label = "";
endIndex = text.indexOf("tag-link");
if (endIndex > 0) {
label = text.substring(endIndex);// sub方法一個值時從這個下标開始到String結尾
beginIndex = label.indexOf(">");
endIndex = label.indexOf("</a>");
label = label.substring(beginIndex + 1, endIndex);// 下标從0開始
} else {
label = "無";
}
// 文章内容
endIndex = text.indexOf("content_views");
String context = text.substring(endIndex + 15);
beginIndex = context.indexOf("<p style=");
if (beginIndex > 0) {
endIndex = context.indexOf(">");
String repetition_no1 = context.substring(beginIndex, endIndex + 1);
context = context.replace(repetition_no1, "");
}
// 除去零散的無用字元串
context = context.replace("<p>", "");
context = context.replace("<br>", "");
context = context.replace("</p>", "");
context = context.replace("<span>", "");
context = context.replace("</span>", "");
context = context.replace("</div>", "");
context = context.replace(" ", "");
System.out.println("文章類型:" + article_type);
System.out.println("标題:" + title_article);
System.out.println("釋出時間:" + time);
System.out.println("釋出者名:" + Name);
System.out.println("個人分類:" + label);
System.out.println("文章内容:" + context);
}
最後列印出篩選後的内容.
文章類型:原
标題:[java爬蟲] 一.從哪裡開始到哪裡開始要哪裡開始
釋出時間:2018年11月20日 08:57:26
釋出者名:張德仁
個人分類:java爬蟲
文章内容:
我不想一邊又一遍的講爬蟲是什麼,我寫這個系列的目的:
1.首先,打破大家學習新事物的誤區;我認為能看到我部落格的人大部分都是成年人,成年人的時間是不多的,寶貴的,是以不能再像以往上學時從基礎開始.
2.其次我要寫部落格就要寫幹貨;直接從資料上下手,将内容都以"子產品化"書寫并使用.
3.将我的所學(自學)的内容分享出來;可能真正的高手與大佬會認為我寫的會比較簡單基礎繁瑣,也歡迎各位指出我的錯誤,在這裡先感謝各位.
我現在正在整理我所有爬蟲項目,準備通過一條線将他們關聯起來,目前整理的為:
1.爬蟲方法與基礎網頁爬取,資料篩選.
2.爬蟲篩選資料都有什麼方法,IO流方法,MySQL資料庫使用.
3.編寫爬蟲測試工具(以後會不斷完善工具).
4.綜合爬取CSDN首頁資料.
5.小總結,并放出我常用的編碼格式轉換等方法.
6.爬取36氪的24小時新聞(涉及尋找網頁接口),常用網頁接口查找方法.
7.爬取網站圖檔,圖檔爬取方法(IO流的使用).
8.使用爬蟲進行網站登入,爬出登入後内容.
9.爬取國内外ip位址,制作ip代理池,更換ip隐藏自己.
10.未完待續.
目前就是這樣,之後我還會整理的.
源碼以及所用的包
度盤: 源碼&包
提取碼: 5jdt
結束
第一次寫這種教學向的部落格,可能有些叙述的不是十厘清楚,還請大家多多指點.
也希望喜愛爬蟲技術的人能夠聚集在一起互相學習互相幫助.
寫爬蟲是我的一個愛好,我也希望能夠通過我的愛好結交更多的人,也能幫助更多的人.
有什麼不明白的可以加我的群,如果我不忙的時候會解決大家的問題.
轉載請标明出處