网络爬虫,也叫网络蜘蛛,有的项目也把它称作“walker”。维基百科所给的定义是“一种系统地扫描互联网,以获取索引为目的的网络程序”。网络上有很多关于网络爬虫的开源项目,其中比较有名的是Heritrix和Apache Nutch。
有时需要在网上搜集信息,如果需要搜集的是获取方法单一而人工搜集费时费力的信息,比如统计一个网站每个月发了多少篇文章、用了哪些标签,为自然语言处理项目搜集语料,或者为模式识别项目搜集图片等等,就需要爬虫程序来完成这样的任务。而且搜索引擎必不可少的组件之一也是网络爬虫。
很多网络爬虫都是用Python,Java或C#实现的。我这里给出的是Java版本的爬虫程序。为了节省时间和空间,我把程序限制在只扫描本博客地址下的网页(也就是http://johnhan.net/但不包括http://johnhany.net/wp-content/下的内容),并从网址中统计出所用的所有标签。只要稍作修改,去掉代码里的限制条件就能作为扫描整个网络的程序使用。或者对输出格式稍作修改,可以作为生成博客sitemap的工具。
代码也可以在这里下载:johnhany/WPCrawler。
环境需求
我的开发环境是Windows7 + Eclipse。
需要XAMPP提供通过url访问MySQL数据库的端口。
还要用到三个开源的Java类库:
Apache HttpComponents 4.3 提供HTTP接口,用来向目标网址提交HTTP请求,以获取网页的内容;
HTML Parser 2.0 用来解析网页,从DOM节点中提取网址链接;
MySQL Connector/J 5.1.27 连接Java程序和MySQL,然后就可以用Java代码操作数据库。
代码
代码位于三个文件中,分别是:crawler.java,httpGet.java和parsePage.java。包名为net.johnhany.wpcrawler。
crawler.java
package net.johnhany.wpcrawler; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class crawler { public static void main(String args[]) throws Exception { String frontpage = "http://johnhany.net/"; Connection conn = null; //connect the MySQL database try { Class.forName("com.mysql.jdbc.Driver"); String dburl = "jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf8"; conn = DriverManager.getConnection(dburl, "root", ""); System.out.println("connection built"); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } String sql = null; String url = frontpage; Statement stmt = null; ResultSet rs = null; int count = 0; if(conn != null) { //create database and table that will be needed try { sql = "CREATE DATABASE IF NOT EXISTS crawler"; stmt = conn.createStatement(); stmt.executeUpdate(sql); sql = "USE crawler"; stmt = conn.createStatement(); stmt.executeUpdate(sql); sql = "create table if not exists record (recordID int(5) not null auto_increment, URL text not null, crawled tinyint(1) not null, primary key (recordID)) engine=InnoDB DEFAULT CHARSET=utf8"; stmt = conn.createStatement(); stmt.executeUpdate(sql); sql = "create table if not exists tags (tagnum int(4) not null auto_increment, tagname text not null, primary key (tagnum)) engine=InnoDB DEFAULT CHARSET=utf8"; stmt = conn.createStatement(); stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } //crawl every link in the database while(true) { //get page content of link "url" httpGet.getByString(url,conn); count++; //set boolean value "crawled" to true after crawling this page sql = "UPDATE record SET crawled = 1 WHERE URL = '" + url + "'"; stmt = conn.createStatement(); if(stmt.executeUpdate(sql) > 0) { //get the next page that has not been crawled yet sql = "SELECT * FROM record WHERE crawled = 0"; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if(rs.next()) { url = rs.getString(2); }else { //stop crawling if reach the bottom of the list break; } //set a limit of crawling count if(count > 1000 || url == null) { break; } } } conn.close(); conn = null; System.out.println("Done."); System.out.println(count); } } }
httpGet.java
package net.johnhany.wpcrawler; import java.io.IOException; import java.sql.Connection; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class httpGet { public final static void getByString(String url, Connection conn) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet(url); System.out.println("executing request " + httpget.getURI()); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse( final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); /* //print the content of the page System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); */ parsePage.parseFromString(responseBody,conn); } finally { httpclient.close(); } } }
parsePage.java
package net.johnhany.wpcrawler; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.htmlparser.Node; import org.htmlparser.Parser; import org.htmlparser.filters.HasAttributeFilter; import org.htmlparser.tags.LinkTag; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; import java.net.URLDecoder; public class parsePage { public static void parseFromString(String content, Connection conn) throws Exception { Parser parser = new Parser(content); HasAttributeFilter filter = new HasAttributeFilter("href"); try { NodeList list = parser.parse(filter); int count = list.size(); //process every link on this page for(int i=0; i<count; i++) { Node node = list.elementAt(i); if(node instanceof LinkTag) { LinkTag link = (LinkTag) node; String nextlink = link.extractLink(); String mainurl = "http://johnhany.net/"; String wpurl = mainurl + "wp-content/"; //only save page from "http://johnhany.net" if(nextlink.startsWith(mainurl)) { String sql = null; ResultSet rs = null; PreparedStatement pstmt = null; Statement stmt = null; String tag = null; //do not save any page from "wp-content" if(nextlink.startsWith(wpurl)) { continue; } try { //check if the link already exists in the database sql = "SELECT * FROM record WHERE URL = '" + nextlink + "'"; stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery(sql); if(rs.next()) { }else { //if the link does not exist in the database, insert it sql = "INSERT INTO record (URL, crawled) VALUES ('" + nextlink + "',0)"; pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pstmt.execute(); System.out.println(nextlink); //use substring for better comparison performance nextlink = nextlink.substring(mainurl.length()); //System.out.println(nextlink); if(nextlink.startsWith("tag/")) { tag = nextlink.substring(4, nextlink.length()-1); //decode in UTF-8 for Chinese characters tag = URLDecoder.decode(tag,"UTF-8"); sql = "INSERT INTO tags (tagname) VALUES ('" + tag + "')"; pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); //if the links are different from each other, the tags must be different //so there is no need to check if the tag already exists pstmt.execute(); } } } catch (SQLException e) { //handle the exceptions System.out.println("SQLException: " + e.getMessage()); System.out.println("SQLState: " + e.getSQLState()); System.out.println("VendorError: " + e.getErrorCode()); } finally { //close and release the resources of PreparedStatement, ResultSet and Statement if(pstmt != null) { try { pstmt.close(); } catch (SQLException e2) {} } pstmt = null; if(rs != null) { try { rs.close(); } catch (SQLException e1) {} } rs = null; if(stmt != null) { try { stmt.close(); } catch (SQLException e3) {} } stmt = null; } } } } } catch (ParserException e) { e.printStackTrace(); } } }
程序原理
所谓“互联网”,是网状结构,任意两个节点间都有可能存在路径。爬虫程序对互联网的扫描,在图论角度来讲,就是对有向图的遍历(链接是从一个网页指向另一个网页,所以是有向的)。常见的遍历方法有深度优先和广度优先两种。相关理论知识可以参考树的遍历:这里和这里。我的程序采用的是广度优先方式。
程序从crawler.java的main()开始运行。
Class.forName("com.mysql.jdbc.Driver"); String dburl = "jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf8"; conn = DriverManager.getConnection(dburl, "root", ""); System.out.println("connection built");
首先,调用DriverManager连接MySQL服务。这里使用的是XAMPP的默认MySQL端口3306,端口值可以在XAMPP主界面看到:
Apache和MySQL都启动之后,在浏览器地址栏输入“http://localhost/phpmyadmin/”就可以看到数据库了。等程序运行完之后可以在这里检查一下运行是否正确。
sql = "CREATE DATABASE IF NOT EXISTS crawler"; stmt = conn.createStatement(); stmt.executeUpdate(sql); sql = "USE crawler"; stmt = conn.createStatement(); stmt.executeUpdate(sql); sql = "create table if not exists record (recordID int(5) not null auto_increment, URL text not null, crawled tinyint(1) not null, primary key (recordID)) engine=InnoDB DEFAULT CHARSET=utf8"; stmt = conn.createStatement(); stmt.executeUpdate(sql); sql = "create table if not exists tags (tagnum int(4) not null auto_increment, tagname text not null, primary key (tagnum)) engine=InnoDB DEFAULT CHARSET=utf8"; stmt = conn.createStatement(); stmt.executeUpdate(sql);
连接好数据库后,建立一个名为“crawler”的数据库,在库里建两个表,一个叫“record”,包含字段“recordID”,“URL”和“crawled”,分别记录地址编号、链接地址和地址是否被扫描过;另一个叫“tags”,包含字段“tagnum”和“tagname”,分别记录标签编号和标签名。
while(true) { httpGet.getByString(url,conn); count++; sql = "UPDATE record SET crawled = 1 WHERE URL = '" + url + "'"; stmt = conn.createStatement(); if(stmt.executeUpdate(sql) > 0) { sql = "SELECT * FROM record WHERE crawled = 0"; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if(rs.next()) { url = rs.getString(2); }else { break; } } }
接着在一个while循环内依次处理表record内的每个地址。每次处理时,把地址url传递给httpGet.getByString(),然后在表record中把crawled改为true,表明这个地址已经处理过。然后寻找下一个crawled为false的地址,继续处理,直到处理到表尾。
这里需要注意的细节是,执行executeQuery()后,得到了一个ResultSet结构rs,rs包含SQL查询返回的所有行和一个指针,指针指向结果中第一行之前的位置,需要执行一次rs.next()才能让rs的指针指向第一个结果,同时返回true,之后每次执行rs.next()都会把指针移到下一个结果上并返回true,直至再也没有结果时,rs.next()的返回值变成了false。
还有一个细节,在执行建库建表、INSERT、UPDATE时,需要用executeUpdate();在执行SELECT时,需要使用executeQuery()。executeQuery()总是返回一个ResultSet,executeUpdate()返回符合查询的行数。
httpGet.java的getByString()类负责向所给的网址发送请求,然后下载网页内容。
HttpGet httpget = new HttpGet(url); System.out.println("executing request " + httpget.getURI()); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse( final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler);
这段代码是HTTPComponents的HTTP Client组件中给出的样例,在很多情况下可以直接使用。这部分代码获得了一个字符串responseBody,里面保存着网页中的全部字符。
接着,就需要把responseBody传递给parsePage.java的parseFromString类提取链接。
Parser parser = new Parser(content); HasAttributeFilter filter = new HasAttributeFilter("href"); try { NodeList list = parser.parse(filter); int count = list.size(); //process every link on this page for(int i=0; i<count; i++) { Node node = list.elementAt(i); if(node instanceof LinkTag) {
在HTML文件中,链接一般都在a标签的href属性中,所以需要创建一个属性过滤器。NodeList保存着这个HTML文件中的所有DOM节点,通过在for循环中依次处理每个节点寻找符合要求的标签,可以把网页中的所有链接提取出来。
然后通过nextlink.startsWith()进一步筛选,只处理以“http://johnhany.net/”开头的链接并跳过以“http://johnhany.net/wp-content/”开头的链接。
sql = "SELECT * FROM record WHERE URL = '" + nextlink + "'"; stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery(sql); if(rs.next()) { }else { //if the link does not exist in the database, insert it sql = "INSERT INTO record (URL, crawled) VALUES ('" + nextlink + "',0)"; pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pstmt.execute();
在表record中查找是否已经存在这个链接,如果存在(rs.next()==true),不做任何处理;如果不存在(rs.next()==false),在表中插入这个地址并把crawled置为false。因为之前recordID设为AUTO_INCREMENT,所以要用 Statement.RETURN_GENERATED_KEYS获取适当的编号。
nextlink = nextlink.substring(mainurl.length()); if(nextlink.startsWith("tag/")) { tag = nextlink.substring(4, nextlink.length()-1); tag = URLDecoder.decode(tag,"UTF-8"); sql = "INSERT INTO tags (tagname) VALUES ('" + tag + "')"; pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pstmt.execute();
去掉链接开头的“http://johnhany.net/”几个字符,提高字符比较的速度。如果含有“tag/”说明其后的字符是一个标签的名字,把这给名字提取出来,用UTF-8编码,保证汉字的正常显示,然后存入表tags。类似地还可以加入判断“article/”,“author/”,或“2013/11/”等对其他链接进行归类。
结果
这是两张数据库的截图,显示了程序的部分结果:
在这里可以获得全部输出结果。可以与本博客的sitemap比较一下,看看如果想在其基础上实现sitemap生成工具,还要做哪些修改。
你好,能做新浪微博评论爬虫吗,通过关键字爬取相关评论
如果只爬虫一个网站的邮箱,我应该怎么改限制?
如果想把查到的网页内容保存到本地,供以后在本地使用,要怎么办?
httpget.java文件里的responseBody就是网页的全部内容
John哥,问下,我用win10、sdk1.8和Intellij idea貌似运行不了程序,是什么原因啊?
XAMPP能运行,而且没有port被占用吗?
博主你好,以转载了你的文章,谢谢!!!
你好!多谢转载!
请问这段代码这样写的意义是什么?如果不用这种形式来写,该如何修改代码?还有就是这种形式的代码要如何称呼? &nb… 阅读更多 »
我想问一下,我用myeclipse ,tomcat ,mysql 可以实现么?然后我想要获取新闻的热搜排行,可以用你这个么
Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connect to johnhany.net:80 [johnhany.net/198.252.107.79] failed: Connection timed out: connect
你好,不知道是不是我jar包导入的版本是不是不对,博主的链接下载不下来。这个报错说我连接不上
麻烦请问一下这个问题解决了没呢?
你的习惯了类名首字母小写???
写爬虫的童鞋可以试试神箭手云爬虫,自带JS渲染、代理ip、验证码识别等功能,还可以发布和导出爬取的数据,生成图表等,都在云端进行,不需要安装开发环境。
您好,看了您写的这个爬虫,我很受教,但是请问一下现在很多网页都在用JS或者AJAX的方法动态生成网站,比如这种 <a href = list.jsp, onclock = "function(2xxxx)">, 需要"用户点击"这种触发事件然后根据2xxxx这种东西从数据库找到被list.jsp覆盖的绝对地址然后将用户直接导航到那个页面,这种情况下要如何处理呢
我试过selenium和webdriver的无头测试,但是这两个货太依赖浏览器了,而且效率奇低无比,博主有什么好方法吗,我的邮箱是 leon.wang@utas.edu.au
如果博主有什么好方法可以赐教一下吗,非常感谢
源码借走了,想做一个抓取电商网站商品详情的网站,有不懂得地方再来讨教你
大神,您好,已经成功搭好环境和下载代码,准备开始大干一场。
同时能你这个eclipse的配色方案比较感兴趣,能分享一下吗。
文章中的代码高亮是一个网页插件产生的,不过eclipse中应该会找到类似的方案:)
嗯 感觉你这个配色很好看
theme 百度就可以了这个插件就有配色方案了
OK, 已经下载到了,非常感谢!!
很想学习这段代码,但发现有关文件无法下载,能否帮忙将所有源代码文件及相关的jar包发送至:xml6401@sina.com
谢!
我想问下,我想用来爬某个官方网站的通知,可以吗。。只怕标题 类容
thank you么么哒~
john哥你好:
最近在做毕业设计,题目是网络搜索引擎的设计与实现,自己也进行过一些学习,看到你写的爬虫代码觉得很受用。学到了很多东西。
下一步还得学习网页预处理部分跟如何实现查询服务,大致的了解了一下处理的方式是通过lucene框架来实现中文的分词,然后就不知该怎么做,能不能请你再大致指点一下,非常感谢!
很抱歉,对搜索引擎的详细原理还不够了解。按我的了解,分词之后应该是关键词提取和分类,然后建立索引库。对搜索结果的排序算法我只了解过PageRank,其他的实在不敢乱说^_^
IK分词器
您好,我想问下这个爬虫可以爬取一些新闻官网的新闻吗?
只要能够利用html标签的名称和属性明确区分爬取内容的网站,应该都是可以的。
你好:我想要获取每个栏目下的文章,怎么解决
如果用Chrome或FireFox浏览器的话,在网页里你想获取的栏目上右键->查看元素(或检查),看一下文章的标签是什么类型和属性,以此修改parsePage.java中的HasAttributeFilter filter。
如何更改访问地址呢,把你访问的博客的地址改成我要访问的,怎么改呢
更改crawler.java第12行的String frontpage