java爬虫
爬虫入门 手写一个java爬虫
本文内容 涞源于 罗刚 老师的 书籍 << 自己动手写网络爬虫一书 >> ;
本文将介绍 1: 网络爬虫的是做什么的? 2: 手动写一个简单的网络爬虫;
1: 网络爬虫是做什么的? 他的主要工作就是 跟据指定的url地址 去发送请求,获得响应, 然后解析响应 , 一方面从响应中查找出想要查找的数据,另一方面从响应中解析出新的URL路径,
然后继续访问,继续解析;继续查找需要的数据和继续解析出新的URL路径 .
这就是网络爬虫主要干的工作. 下面是流程图:
通过上面的流程图 能大概了解到 网络爬虫 干了哪些活 ,根据这些 也就能设计出一个简单的网络爬虫出来.
一个简单的爬虫 必需的功能:
1: 发送请求和获取响应的功能 ;
2: 解析响应的功能 ;
3: 对 过滤出的数据 进行存储 的功能 ;
4: 对解析出来的URL路径 处理的功能 ;
下面是包结构:
下面就上代码:
requestAndresponseTool 类: 主要方法: 发送请求 返回响应 并把 响应 封装成 page 类 ;
package com.etoak.crawl.page; import org.apache.commons.httpclient.DefaultHttpMethodretryhandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import java.io.IOException; public class RequestAndResponseTool { public static Page sendRequstAndGetResponse(String url) { Page page = null; // 1.生成 HttpClinet 对象并设置参数 HttpClient httpClient = new HttpClient(); // 设置 HTTP 连接超时 5s httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); // 2.生成 GetMethod 对象并设置参数 GetMethod getMethod = new GetMethod(url); // 设置 get 请求超时 5s getMethod.getParams().setparameter(HttpMethodParams.SO_TIMEOUT, 5000); // 设置请求重试处理 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // 3.执行 HTTP GET 请求 try { int statusCode = httpClient.executeMethod(getMethod); // 判断访问的状态码 if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } // 4.处理 HTTP 响应内容 byte[] responseBody = getMethod.getResponseBody();// 读取为字节 数组 String contentType = getMethod.getResponseHeader("Content-Type").getValue(); // 得到当前返回类型 page = new Page(responseBody,url,contentType); //封装成为页面 } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有问题 System.out.println("Please check your provided http address!"); e.printstacktrace(); } catch (IOException e) { // 发生网络异常 e.printStackTrace(); } finally { // 释放连接 getMethod.releaseConnection(); } return page; } }
page 类: 主要作用: 保存响应的相关内容 对外提供访问方法;
package com.etoak.crawl.page; import com.etoak.crawl.util.CharsetDetector; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.UnsupportedEncodingException; /* * page * 1: 保存获取到的响应的相关内容; * */ public class Page { private byte[] content ; private String html ; //网页源码字符串 private Document doc ;//网页Dom文档 private String charset ;//字符编码 private String url ;//url路径 private String contentType ;// 内容类型 public Page(byte[] content , String url , String contentType){ this.content = content ; this.url = url ; this.contentType = contentType ; } public String getCharset() { return charset; } public String getUrl(){return url ;} public String getContentType(){ return contentType ;} public byte[] getContent(){ return content ;} /** * 返回网页的源码字符串 * * @return 网页的源码字符串 */ public String getHtml() { if (html != null) { return html; } if (content == null) { return null; } if(charset==null){ charset = CharsetDetector.guessEncoding(content); // 根据内容来猜测 字符编码 } try { this.html = new String(content, charset); return html; } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); return null; } } /* * 得到文档 * */ public Document getDoc(){ if (doc != null) { return doc; } try { this.doc = Jsoup.parse(getHtml(), url); return doc; } catch (Exception ex) { ex.printStackTrace(); return null; } } }
PageparserTool: 类 主要作用 提供了 根据选择器来选取元素 属性 等方法 ;
package com.etoak.crawl.page; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class PageParserTool { /* 通过选择器来选取页面的 */ public static Elements select(Page page , String cssSelector) { return page.getDoc().select(cssSelector); } /* * 通过css选择器来得到指定元素; * * */ public static Element select(Page page , String cssSelector, int index) { Elements eles = select(page , cssSelector); int realIndex = index; if (index < 0) { realIndex = eles.size() + index; } return eles.get(realIndex); } /** * 获取满足选择器的元素中的链接 选择器cssSelector必须定位到具体的超链接 * 例如我们想抽取id为content的p中的所有超链接,这里 * 就要将cssSelector定义为p[id=content] a * 放入set 中 防止重复; * @param cssSelector * @return */ public static Set<String> getLinks(Page page ,String cssSelector) { Set<String> links = new HashSet<String>() ; Elements es = select(page , cssSelector); Iterator iterator = es.iterator(); while(iterator.hasNext()) { Element element = (Element) iterator.next(); if ( element.hasAttr("href") ) { links.add(element.attr("abs:href")); }else if( element.hasAttr("src") ){ links.add(element.attr("abs:src")); } } return links; } /** * 获取网页中满足指定css选择器的所有元素的指定属性的集合 * 例如通过getAttrs("img[src]","abs:src")可获取网页中所有图片的链接 * @param cssSelector * @param attrName * @return */ public static ArrayList<String> getAttrs(Page page , String cssSelector, String attrName) { ArrayList<String> result = new ArrayList<String>(); Elements eles = select(page ,cssSelector); for (Element ele : eles) { if (ele.hasAttr(attrName)) { result.add(ele.attr(attrName)); } } return result; } }
Link 包 ;
Links 类: 两个属性: 一个是存放 已经访问的url集合的set ; 一个是存放待访问url集合的 queue ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
LinkFilter 接口: 可以起过滤作用;
package com.etoak.crawl.link; public interface LinkFilter { public boolean accept(String url); }
util 工具类
CharsetDetector 类; 获取字符编码
/* * Copyright (C) 2014 hu * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTAbility or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.etoak.crawl.util; import org.mozilla.universalchardet.UniversalDetector; import java.io.UnsupportedEncodingException; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 字符集自动检测 * * @author hu */ public class CharsetDetector { //从Nutch借鉴的网页编码检测代码 private static final int CHUNK_SIZE = 2000; private static Pattern metaPattern = pattern.compile( "<meta\\s+([^>]*http-equiv=(\"|')?content-type(\"|')?[^>]*)>", Pattern.CASE_INsensitive); private static Pattern charsetPattern = Pattern.compile( "charset=\\s*([a-z][_\\-0-9a-z]*)", Pattern.CASE_INSENSITIVE); private static Pattern charsetPatternHTML5 = Pattern.compile( "<meta\\s+charset\\s*=\\s*[\"']?([a-z][_\\-0-9a-z]*)[^>]*>", Pattern.CASE_INSENSITIVE); //从Nutch借鉴的网页编码检测代码 private static String guessEncodingByNutch(byte[] content) { int length = Math.min(content.length, CHUNK_SIZE); String str = ""; try { str = new String(content, "ascii"); } catch (UnsupportedEncodingException e) { return null; } Matcher metaMatcher = metaPattern.matcher(str); String encoding = null; if (metaMatcher.find()) { Matcher charsetMatcher = charsetPattern.matcher(metaMatcher.group(1)); if (charsetMatcher.find()) { encoding = new String(charsetMatcher.group(1)); } } if (encoding == null) { metaMatcher = charsetPatternHTML5.matcher(str); if (metaMatcher.find()) { encoding = new String(metaMatcher.group(1)); } } if (encoding == null) { if (length >= 3 && content[0] == (byte) 0xEF && content[1] == (byte) 0xBB && content[2] == (byte) 0xBF) { encoding = "UTF-8"; } else if (length >= 2) { if (content[0] == (byte) 0xFF && content[1] == (byte) 0xFE) { encoding = "UTF-16LE"; } else if (content[0] == (byte) 0xFE && content[1] == (byte) 0xFF) { encoding = "UTF-16BE"; } } } return encoding; } /** * 根据字节数组,猜测可能的字符集,如果检测失败,返回utf-8 * * @param bytes 待检测的字节数组 * @return 可能的字符集,如果检测失败,返回utf-8 */ public static String guessEncodingByMozilla(byte[] bytes) { String DEFAULT_ENCODING = "UTF-8"; UniversalDetector detector = new UniversalDetector(null); detector.handleData(bytes, 0, bytes.length); detector.dataEnd(); String encoding = detector.getDetectedCharset(); detector.reset(); if (encoding == null) { encoding = DEFAULT_ENCODING; } return encoding; } /** * 根据字节数组,猜测可能的字符集,如果检测失败,返回utf-8 * @param content 待检测的字节数组 * @return 可能的字符集,如果检测失败,返回utf-8 */ public static String guessEncoding(byte[] content) { String encoding; try { encoding = guessEncodingByNutch(content); } catch (Exception ex) { return guessEncodingByMozilla(content); } if (encoding == null) { encoding = guessEncodingByMozilla(content); return encoding; } else { return encoding; } } }
FileTool 文件下载类:
package com.etoak.crawl.util; import com.etoak.crawl.page.Page; import java.io.Dataoutputstream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /* 本类主要是 下载那些已经访问过的文件*/ public class FileTool { private static String dirPath; /** * getMethod.getResponseHeader("Content-Type").getValue() * 根据 URL 和网页类型生成需要保存的网页的文件名,去除 URL 中的非文件名字符 */ private static String getFileNameByUrl(String url, String contentType) { //去除 http:// url = url.substring(7); //text/html 类型 if (contentType.indexof("html") != -1) { url = url.replaceAll("[\\?/:*|<>\"]", "_") + ".html"; return url; } //如 APPlication/pdf 类型 else { return url.replaceAll("[\\?/:*|<>\"]", "_") + "." + contentType.substring(contentType.lastIndexOf("/") + 1); } } /* * 生成目录 * */ private static void mkdir() { if (dirPath == null) { dirPath = Class.class.getClass().getResource("/").getPath() + "temp\\"; } File fileDir = new File(dirPath); if (!fileDir.exists()) { fileDir.mkdir(); } } /** * 保存网页字节数组到本地文件,filePath 为要保存的文件的相对地址 */ public static void saveTolocal(Page page) { mkdir(); String fileName = getFileNameByUrl(page.getUrl(), page.getContentType()) ; String filePath = dirPath + fileName ; byte[] data = page.getContent(); try { //Files.lines(Paths.get("D:\\jd.txt"), StandardCharsets.UTF_8).foreach(System.out::println); DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(filePath))); for (int i = 0; i < data.length; i++) { out.write(data[i]); } out.flush(); out.close(); System.out.println("文件:"+ fileName + "已经被存储在"+ filePath ); } catch (IOException e) { e.printStackTrace(); } } }
RegexRule 正则表达式类;
/* * Copyright (C) 2014 hu * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.etoak.crawl.util; import java.util.ArrayList; import java.util.regex.Pattern; /** * * @author hu */ public class RegexRule { public RegexRule(){ } public RegexRule(String rule){ addRule(rule); } public RegexRule(ArrayList<String> rules){ for (String rule : rules) { addRule(rule); } } public boolean isEmpty(){ return positive.isEmpty(); } private ArrayList<String> positive = new ArrayList<String>(); private ArrayList<String> negative = new ArrayList<String>(); /** * 添加一个正则规则 正则规则有两种,正正则和反正则 * URL符合正则规则需要满足下面条件: 1.至少能匹配一条正正则 2.不能和任何反正则匹配 * 正正则示例:+a.*c是一条正正则,正则的内容为a.*c,起始加号表示正正则 * 反正则示例:-a.*c时一条反正则,正则的内容为a.*c,起始减号表示反正则 * 如果一个规则的起始字符不为加号且不为减号,则该正则为正正则,正则的内容为自身 * 例如a.*c是一条正正则,正则的内容为a.*c * @param rule 正则规则 * @return 自身 */ public RegexRule addRule(String rule) { if (rule.length() == 0) { return this; } char pn = rule.charAt(0); String realrule = rule.substring(1); if (pn == '+') { addPositive(realrule); } else if (pn == '-') { addNegative(realrule); } else { addPositive(rule); } return this; } /** * 添加一个正正则规则 * @param positiveregex * @return 自身 */ public RegexRule addPositive(String positiveregex) { positive.add(positiveregex); return this; } /** * 添加一个反正则规则 * @param negativeregex * @return 自身 */ public RegexRule addNegative(String negativeregex) { negative.add(negativeregex); return this; } /** * 判断输入字符串是否符合正则规则 * @param str 输入的字符串 * @return 输入字符串是否符合正则规则 */ public boolean satisfy(String str) { int state = 0; for (String nregex : negative) { if (Pattern.matches(nregex, str)) { return false; } } int count = 0; for (String pregex : positive) { if (Pattern.matches(pregex, str)) { count++; } } if (count == 0) { return false; } else { return true; } } }
主类:
MyCrawler :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
运行结果:
源码下载链接: https://pan.baidu.com/s/1ge7Nkzx 下载密码: mz5b文章主要参考: 1: 自己动手写网络爬虫;
2: https://github.com/CrawlScript/WebCollector
WebCollector是一个无须配置、便于二次开发的java爬虫框架(内核),它提供精简的的API,只需少量代码即可实现一个功能强大的爬虫。WebCollector-Hadoop是WebCollector的Hadoop版本,支持分布式爬取。
相关阅读
java注释使用及error occurred during initialization
JAVA注释 java语言注释有三种——单行注释,多行注释以及文档注释。单行注释:只需要将双斜线//放在这一行的最前面即可多行注释:以/*
写在前面 之前二胖分享的代码多是Python的,不少粉丝在公众号后台私信我爬虫相关的技术,二胖发现很多人在潜意识里认为Python就等于
/Users/rimi/Desktop/p1805/venv/bin/python /Users/rimi/Desktop/p1805/第4周/第4周笔记/request.py<!doctype html> 腾讯首
java.io.IOException: UT000036: Connection terminat
java.lang.RuntimeException: java.io.IOException: UT000036: Connection terminated parsing multipart data at io.undertow.
JPA使用指南 javax.persistence的注解配置
转自http://67566894.iteye.com/blog/659829 @SuppressWarnings("serial") @Entity @Table(name="T_X") public class X impleme