java网页正则表达式爬虫(用纯java代码在相应的网页中查看自己想要的内容)

在吃夜宵的时候利用短暂的时间做个爬虫小程序,代码没时间优化,但是功能全实现了。eclipse的项目结构如下图:


代码如下:


package com.jiaxun.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
				//小询
public class EmailSpider {
	//获取网页内容(如这个网站:"https://www.douban.com/group/topic/23934101/?start=600")
	public static String getPageContent(String myurl){//在此导入网址链接
		StringBuffer sb = new StringBuffer();
		URL url =null;
		Scanner scanner = null;
		try {
			url = new URL(myurl);
			URLConnection conn = url.openConnection();
			
			scanner = new Scanner(conn.getInputStream());
			while (scanner.hasNextLine()) {
				String content = scanner.nextLine();
				sb.append(content).append("\r\n");
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			scanner.close();
		}
		return sb.toString();
	}
	//将网页内容输出到本地文本文件
	public static void saveToLocalFile(){
		String pageContent = getPageContent("https://www.douban.com/group/topic/23934101/?start=600");
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("pagecontent.txt")));
			pw.println(pageContent);
			pw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			pw.close();
		}
	}
	//将邮箱找出来并保存到本地文件中
	public static void emailSpider(String fileName){//相对路径为pagecontent.txt
		StringBuffer sb = new StringBuffer();
		Scanner scanner = null;
		PrintWriter pw = null;
		try {
			scanner = new Scanner(new FileInputStream(fileName));
			while (scanner.hasNextLine()) {
				String content = scanner.nextLine();
				sb.append(content);
			}
			String parseMails = parseMails(sb.toString());
			pw = new PrintWriter("mails.txt");
			pw.println(parseMails);
			pw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			pw.close();
			scanner.close();
		}
		
		
	}
	//邮箱匹配
	public static String parseMails(String line){
		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
		Matcher matcher = pattern.matcher(line);
		while(matcher.find()){
			sb.append(matcher.group()).append("\r\n");
		}
		return sb.toString();
	}
	public static void main(String[] args) {
		saveToLocalFile();
		emailSpider("pagecontent.txt");
	}
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值