首页 > 编程语言 > 详细

java——手写爬虫

时间:2019-03-10 17:24:26      阅读:121      评论:0      收藏:0      [点我收藏+]
本文测试的是网易的地址 你可以自行测试

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NetPaChong {

	/**
	 * 正则表达式 写网络爬虫
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String text=getContent("http://www.163.com");
		
		System.out.println(text.length());
		
		Pattern p=Pattern.compile("href=\"([\\w\\s./:]+?)\"");
		Matcher m=p.matcher(text);
		while(m.find()){
			System.out.println(m.group());
		}
	}
	
	
	public static String getContent(String strUrl){
		StringBuffer sb=new StringBuffer();
		try {
			URL url=new URL(strUrl);
			BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
			String temp="";
			while((temp=br.readLine())!=null){
				sb.append(temp);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

}

  

java——手写爬虫

原文:https://www.cnblogs.com/qurui1997/p/10505921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!