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


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中tomcat memecached session 共享同步問題的解決辦法
這篇文章主要介紹了Java中tomcat memecached session 共享同步問題的解決辦法的相關(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)用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09
SpringBoot創(chuàng)建并簡單使用的實現(xiàn)
這篇文章主要介紹了SpringBoot創(chuàng)建并簡單使用的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10

