爬虫原理:我们一般访问网页时,都会把html源码下载到本地,因此我们就可以模拟网页的请求方式,将得到的html源码以流的形式写入缓存,然后再通过正则表达式或者其他方法进行数据匹配,进而得到我们想要的数据。
所用到的工具类:URL:打开网页链接,即执行访问url的功能
URLConnection:获取访问后下载的html源代码
Pattern:java支持正则表达式的类
Matcher:执行正则表达式的匹配
源代码如下:
package spider;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import DB.MysqlConfig;
import DB.Zsgc;
import entity.ContentNews;
/**
* @author HRX
* @version 创建时间:2018年10月16日 下午4:50:31
* 类说明
*/
public class CatchContent {
public static void main(String[] args) throws IOException, ParseException {
Spider("http://www.runoob.com/linux/linux-shell.html");
}
public static void Spider(String urlString) throws IOException, ParseException{
System.out.println("------------爬取开始执行-----------");
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection conn = url.openConnection();
//设置请求超时为15s
conn.setConnectTimeout(15*1000);
// 输入流
InputStream in = conn.getInputStream();
System.out.println("----------爬取完毕---------");
System.out.println("------------开始转化数据-----------");
String str = new BufferedReader(new InputStreamReader(in))
.lines().collect(Collectors.joining(System.lineSeparator()));
System.out.println("------------开始匹配数据-----------");
// str = removeHtmlTag(str);
System.out.println(str);
insert(str);
in.close();
}
public static String removeHtmlTag(String inputString) {
if (inputString == null){
return null;
} else {
// 含html标签的字符串
String htmlStr = inputString;
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
java.util.regex.Pattern p_special;
java.util.regex.Matcher m_special;
java.util.regex.Pattern ep_special;
java.util.regex.Matcher em_special;
try {
//定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
//定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
// 定义HTML标签的正则表达式
String regEx_html = "<[^>]+>";
// 定义一些特殊字符的正则表达式 如:
String regEx_special = "\\&[a-zA-Z]{1,10};";
//去除除p之外的标签
String regEx_exceptP = "(?!<(td|p|span).*?>)</td*?>";//"<(p*?)[^>]*>.*?";
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
// 过滤script标签
htmlStr = m_script.replaceAll("");
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
// 过滤style标签
htmlStr = m_style.replaceAll("");
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
//过滤所有html标签
htmlStr = m_style.replaceAll("");
ep_special = Pattern.compile(regEx_exceptP, Pattern.CASE_INSENSITIVE);
em_special = ep_special.matcher(htmlStr);
// 过滤除p之外的标签
htmlStr = em_special.replaceAll("");
p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
m_special = p_special.matcher(htmlStr);
// 过滤特殊标签
htmlStr = m_special.replaceAll("");
textStr = htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return textStr;
}
}
}