Java爬取網(wǎng)站源代碼和鏈接代碼實(shí)例
1. 網(wǎng)絡(luò)爬蟲是一個(gè)自動(dòng)提取網(wǎng)頁(yè)的程序,它為搜索引擎從萬(wàn)維網(wǎng)上下載網(wǎng)頁(yè),是搜索引擎的重要組成。傳統(tǒng)爬蟲從一個(gè)或若干初始網(wǎng)頁(yè)的URL開始,獲得初始網(wǎng)頁(yè)上的URL,在抓取網(wǎng)頁(yè)的過(guò)程中,不斷從當(dāng)前頁(yè)面上抽取新的URL放入隊(duì)列,直到滿足系統(tǒng)的一定停止條件。
所以主要使用遞歸遍歷完成對(duì)每個(gè)網(wǎng)頁(yè)內(nèi)鏈接的獲取和源碼的獲取,然后剔除重復(fù)鏈接
數(shù)據(jù)爬取后主要使用txt文件儲(chǔ)存,根據(jù)網(wǎng)址的路徑生成想應(yīng)文件路徑
2.代碼
package com.test;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* java實(shí)現(xiàn)爬蟲
*/
public class SpiderDemo1 {
//網(wǎng)站主網(wǎng)頁(yè)鏈接
private final static String theURL = "http://www.jyyishu.cn";
//今日日期,用于標(biāo)記日志
private final static String theTIME = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//網(wǎng)頁(yè)鏈接文件路徑
private final static String theFILE = "L:/html/jy1/" + theTIME + "/URL.txt";
//網(wǎng)頁(yè)源碼路徑
private final static String thePATH = "L:/html/jy1/" + theTIME + "/code";
//正則表達(dá)式,用于判斷是否是一個(gè)網(wǎng)址
private final static String theREGEX= "(http|https)://[\\w+\\.?/?]+\\.[A-Za-z]+";
/**
* 啟動(dòng)類
* @param args
*/
public static void main(String[] args) {
found();
System.out.println("網(wǎng)站爬取完成");
}
public static void found() {
PrintWriter pw = null;
try{
//創(chuàng)建文件目錄
File fileDir = new File(thePATH);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//創(chuàng)建網(wǎng)站網(wǎng)頁(yè)鏈接文件
pw = new PrintWriter(new FileWriter(theFILE),true);
//使用遞歸遍歷網(wǎng)站的每個(gè)網(wǎng)頁(yè)
spiderURL(theURL, pw);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(pw != null) {
pw.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* 爬取該網(wǎng)頁(yè)源碼和網(wǎng)頁(yè)內(nèi)連接
* @param url 該網(wǎng)頁(yè)網(wǎng)址
* @param tpw 對(duì)網(wǎng)站網(wǎng)頁(yè)網(wǎng)址文件連接的io流
*/
public static void spiderURL(String url, PrintWriter tpw){
URL realURL=null;
URLConnection connection=null;
BufferedReader br=null;
PrintWriter pw=null;
PrintWriter pw1=null;
Pattern pattern=Pattern.compile(theREGEX);
try{
realURL=new URL(url);
connection=realURL.openConnection();
//生成文件夾
String src = thePATH + url.substring(theURL.length());
File fileDir = new File(src);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//生成源代碼文件
pw = new PrintWriter(new FileWriter(src + "/Test.txt"),true);
pw1 = tpw;
//爬取網(wǎng)頁(yè)文件
br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=null;
while((line=br.readLine())!=null){
//把爬取的源碼寫入文件
pw.println(line);
System.out.println("爬取網(wǎng)頁(yè)" + url + "成功");
Matcher matcher=pattern.matcher(line);
//判斷是否是一個(gè)網(wǎng)址
while(matcher.find()){
//判斷網(wǎng)址是否以網(wǎng)站主網(wǎng)址為前綴,防止爬到其他網(wǎng)站,并判斷是否和原先網(wǎng)址重復(fù)
if(matcher.group().startsWith(theURL) && examine(matcher.group())) {
//把爬取的網(wǎng)址寫入文件
pw1.println(matcher.group());
spiderURL(matcher.group(), pw1);
}
}
System.out.println("網(wǎng)頁(yè)" + url + "內(nèi)鏈接爬取完成");
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(br != null) {
br.close();
}
if(pw != null) {
pw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 判斷是否和以儲(chǔ)存網(wǎng)址相同
* @param str 需判斷的網(wǎng)址
* @return 是否重復(fù)
*/
public static boolean examine(String str) {
BufferedReader br = null;
String str1;
try {
br = new BufferedReader(new FileReader(theFILE));
// //針對(duì)該網(wǎng)站無(wú)用網(wǎng)頁(yè)的屏蔽
// if(str.startsWith("http://www.jyyishu.cn/artnews/")) {
// return false;
// }
//循環(huán)文件中每一行的網(wǎng)址,判斷是否重復(fù),重復(fù)則退出
while((str1 = br.readLine()) != null) {
if(str.equals(str1)) {
return false;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
2. 爬取后的數(shù)據(jù)
部分鏈接:

網(wǎng)頁(yè)數(shù)據(jù):


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中tomcat memecached session 共享同步問(wèn)題的解決辦法
這篇文章主要介紹了Java中tomcat memecached session 共享同步問(wèn)題的解決辦法的相關(guān)資料,需要的朋友可以參考下2015-10-10
Java 內(nèi)置Http Server構(gòu)建web應(yīng)用案例詳解
這篇文章主要介紹了Java 內(nèi)置Http Server構(gòu)建web應(yīng)用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
SpringBoot創(chuàng)建并簡(jiǎn)單使用的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot創(chuàng)建并簡(jiǎn)單使用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例
這篇文章主要介紹了JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-11-11

