話不多說直接乾貨
目錄
我用到的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
結束
第一次寫這種教學向的博客,可能有些敘述的不是十分清楚,還請大家多多指點.
也希望喜愛爬蟲技術的人能夠聚集在一起相互學習相互幫助.
寫爬蟲是我的一個愛好,我也希望能夠通過我的愛好結交更多的人,也能幫助更多的人.
有什麼不明白的可以加我的羣,如果我不忙的時候會解決大家的問題.
轉載請標明出處
也歡迎加入我的羣和我一起探討java爬蟲的樂趣
324757127(新建的羣)
有數據定製的爬蟲需求也可以加我的qq10554214 標註:數據定製