java网页 所有 链接_java爬取网站中所有网页的源代码和链接

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实现爬虫*/

public classSpiderDemo1 {//网站主网页链接

private final static String theURL = "http://www.jyyishu.cn";//今日日期,用于标记日志

private final static String theTIME = new SimpleDateFormat("yyyy-MM-dd").format(newDate());//网页链接文件路径

private final static String theFILE = "L:/html/jy1/" + theTIME + "/URL.txt";//网页源码路径

private final static String thePATH = "L:/html/jy1/" + theTIME + "/code";//正则表达式,用于判断是否是一个网址

private final static String theREGEX= "(http|https)://[\\w+\\.?/?]+\\.[A-Za-z]+";/**

* 启动类

* @param args*/

public static voidmain(String[] args) {

found();

System.out.println("网站爬取完成");

}public static voidfound() {

PrintWriter pw= null;try{//创建文件目录

File fileDir = newFile(thePATH);if (!fileDir.exists()) {

fileDir.mkdirs();

}//创建网站网页链接文件

pw = new PrintWriter(new FileWriter(theFILE),true);//使用递归遍历网站的每个网页

spiderURL(theURL, pw);

}catch(IOException e) {

e.printStackTrace();

}finally{try{if(pw != null) {

pw.close();

}

}catch(Exception e) {

e.printStackTrace();

}

}

}/**

* 爬取该网页源码和网页内连接

* @param url 该网页网址

* @param tpw 对网站网页网址文件连接的io流*/

public static voidspiderURL(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=newURL(url);

connection=realURL.openConnection();//生成文件夹

String src = thePATH +url.substring(theURL.length());

File fileDir= newFile(src);if (!fileDir.exists()) {

fileDir.mkdirs();

}//生成源代码文件

pw = new PrintWriter(new FileWriter(src + "/Test.txt"),true);

pw1=tpw;//爬取网页文件

br=new BufferedReader(newInputStreamReader(connection.getInputStream()));

String line=null;while((line=br.readLine())!=null){//把爬取的源码写入文件

pw.println(line);

System.out.println("爬取网页" + url + "成功");

Matcher matcher=pattern.matcher(line);//判断是否是一个网址

while(matcher.find()){//判断网址是否以网站主网址为前缀,防止爬到其他网站,并判断是否和原先网址重复

if(matcher.group().startsWith(theURL) &&examine(matcher.group())) {//把爬取的网址写入文件

pw1.println(matcher.group());

spiderURL(matcher.group(), pw1);

}

}

System.out.println("网页" + url + "内链接爬取完成");

}

}catch(Exception e){

e.printStackTrace();

}finally{try{if(br != null) {

br.close();

}if(pw != null) {

pw.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}/**

* 判断是否和以储存网址相同

* @param str 需判断的网址

* @return 是否重复*/

public staticboolean examine(String str) {

BufferedReader br= null;

String str1;try{

br= new BufferedReader(newFileReader(theFILE));// //针对该网站无用网页的屏蔽//if(str.startsWith("http://www.jyyishu.cn/artnews/")) {//return false;//}//循环文件中每一行的网址,判断是否重复,重复则退出

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值