节前一个误操作把 mysql 中 record 表和 movie 表都清空了,显然我是没有做什么 mysql 备份的。所以,索性我把所有的表数据都清空的,一夜回到解放前……
项目地址:
在上一个版本中,record 表存储了 7 万多条记录,爬取的有 4 万多条,但是可以明显的发现爬取的数据量越多的时候,机子就越卡。又一次报错,是有关 JDBC 的,还有一次机子跑卡死了。
仔细一琢磨,上个版本的爬虫程序与数据库的读写次数太频繁,存在以下问题:
1. 程序运行,从种子地址开始,对于每次爬取的网站地址先查询数据库是否存在该条记录,如果不存在,则立即插入;
2. 当前网站地址爬取完毕后,查找数据库从中取出第一个 crawled 为 0 的记录进行爬取,每次只取一条;
3. 存储电影详情页记录以及短评数据都是采用解析一条则立即存储到数据库。
显然,上面的这种方式是一目了然的效率低下,所以今天下午对相关代码进行改造,部分实现了批量插入,尽可能减少与数据库的交互,从而降低时空成本。
在 git clone 完项目后,发现一个很诡异的现象,JewelCrawler 每次都是爬取种子地址,并没有一次查询数据库中 crawled 字段为 0 的记录进行一一爬取,但是之前在本机上是完美运行的,可能是在 push 代码前做了改动影响运行了。
既然问题出现了,就顺着这个版本看看,最终发现问题的原因是对于种子网址并没有存储到 mysql 的 record 表中,所以在 DoubanCrawler 类中
- //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 > Constants.maxCycle || url == null) {
- break;
- }
- }
执行 stmt.executeUpdate(sql) > 0 是返回的值为 0,从而不会从数据库中读取 crawled 为 0 的记录,最后就一直在 while 的循环中爬取种子网站。
解决方法:对于种子网站既然没有存储到 record 的操作,那么就对种子网站做特殊处理,将 if 的判断条件改为 if (stmt.executeUpdate(sql) > 0 || frontPage.equals(url)),这样对于种子网站即使没有 update 更新成功操作仍然可以进入读取数据库 crawled 为 0 的操作。
针对第一个问题,采用批量插入操作
实现思路:对于当前爬取的网站地址,解析网页源码,提取出所有的 link,对于符合正则表达式过滤的 link,将其存到一个 list 集合中。遍历完当前网址的所有 link 后,将符合条件的 link 批量存储到数据库中。
具体实现如下
- public static void parseFromString(String content, Connection conn) throws Exception {
- Parser parser = new Parser(content);
- HasAttributeFilter filter = new HasAttributeFilter("href");
- String sql1 = null;
- ResultSet rs1 = null;
- PreparedStatement pstmt1 = null;
- Statement stmt1 = null;
- List<String> nextLinkList = new ArrayList<String>();
- int rowCount = 0;
- sql1 = "select count(*) as rowCount from record";
- stmt1 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
- rs1 = stmt1.executeQuery(sql1);
- if (rs1.next()) {
- rowCount = rs1.getString("rowCount") != null ? Integer.parseInt(rs1.getString("rowCount")) : 0;
- }
- if (rowCount <= Constants.maxCycle) { //once rowCount is bigger than maxCycle, the new crawled link will not insert into record table
- 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 = Constants.MAINURL;
- if (nextLink.startsWith(mainUrl)) {
- //check if the link already exists in the database
- sql1 = "SELECT * FROM record WHERE URL = '" + nextLink + "'";
- stmt1 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
- rs1 = stmt1.executeQuery(sql1);
- if (rs1.next()) {
- } else {
- Pattern moviePattern = Pattern.compile(Constants.MOVIE_REGULAR_EXP);
- Matcher movieMatcher = moviePattern.matcher(nextLink);
- Pattern commentPattern = Pattern.compile(Constants.COMMENT_REGULAR_EXP);
- Matcher commentMatcher = commentPattern.matcher(nextLink);
- if (movieMatcher.find() || commentMatcher.find()) {
- nextLinkList.add(nextLink);
- }
- }
- }
- }
- }
- if (nextLinkList.size() > 0) {
- conn.setAutoCommit(false);
- //if the link does not exist in the database, insert it
- sql1 = "INSERT INTO record (URL, crawled) VALUES (?,0)";
- pstmt1 = conn.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
- for (String nextLinkStr : nextLinkList) {
- pstmt1.setString(1, nextLinkStr);
- pstmt1.addBatch();
- System.out.println(nextLinkStr);
- }
- pstmt1.executeBatch();
- conn.commit();
- }
- } catch (Exception e) {
- //handle the exceptions
- e.printStackTrace();
- System.out.println("SQLException: " + e.getMessage());
- } finally {
- //close and release the resources of PreparedStatement, ResultSet and Statement
- if (pstmt1 != null) {
- try {
- pstmt1.close();
- } catch (SQLException e2) {
- }
- }
- pstmt1 = null;
- if (rs1 != null) {
- try {
- rs1.close();
- } catch (SQLException e1) {
- }
- }
- rs1 = null;
- if (stmt1 != null) {
- try {
- stmt1.close();
- } catch (SQLException e3) {
- }
- }
- stmt1 = null;
- }
- }
- }
1. 通过正则匹配,找到符合条件的 link,并添加到 nextLinkList 集合中
2. 遍历完后,将数据存到数据库中
3. 在批量操作中,使用了 addBatch() 方法和 executeBatch() 方法,注意需要添加 conn.setAutoCommit(false); 以及 conn.commit() 表示手动提交。
针对第二个问题,采用一次查询多条记录
实现思路:将每次只查询一条记录,改为每次查询 10 条记录,并将这 10 条记录存放到 list 集合中,并将原来的 String 类型的 url 改为 list 类型的 urlList 传入到 DouBanHttpGetUtil.getByString() 方法里。这样即减少了与数据库的交互,同时也减少了对于 getByString 方法的调用。
具体实现如下
- public static void main(String args[]) throws Exception {
- //load and read seed file
- List<String> seedList = LoadSeed.loadSeed();
- if (seedList == null) {
- log.info("No seed to crawl, please check again");
- return;
- }
- String frontPage = seedList.get(0);
- //connect database mysql
- Connection conn = DBUtils.connectDB();
- //create tables to store crawled data
- DBUtils.createTables();
- String sql = null;
- String url = frontPage;
- Statement stmt = null;
- ResultSet rs = null;
- int count = 0;
- List<String> urlList = new ArrayList<String>();
- urlList.add(url);
- //crawl every link in the database
- while (true) {
- //get page content of link "url"
- DouBanHttpGetUtil.getByString(urlList, conn);
- count++;
- //set boolean value "crawled" to true after crawling this page
- //TODO batch update
- int result = 0;
- conn.setAutoCommit(true);
- for (String urlStr : urlList) {
- sql = "UPDATE record SET crawled = 1 WHERE URL = '" + urlStr + "'";
- stmt = conn.createStatement();
- stmt.executeUpdate(sql);
- }
- urlList.clear();//empty for every loop
- if (stmt.executeUpdate(sql) > 0 || frontPage.equals(url)) {
- //get the next page that has not been crawled yet
- sql = "SELECT * FROM record WHERE crawled = 0 limit 10";
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- url = rs.getString(2);
- urlList.add(url);
- }
- //set a limit of crawling count
- if (rs.next() || count > Constants.maxCycle || url == null) {
- break;
- }
- }
- }
- conn.close();
- conn = null;
- System.out.println("Done.");
- System.out.println(count);
- }
注意: 1. 这里采用每次读取 10 条记录,相应的也需要将这 10 条记录的 crawled 字段更新为 1,表示爬取过。
2. mysql 不支持 top 10 * 这样的语法,但是可以通过代码中所示的 limit 10 的方式取出数据。
3. 添加 conn.setAutoCommit(true); 表示更新操作设置为自动提交,这样就可以解决虽然程序执行成功但是数据没有更新到数据库的现象。
针对第三个问题,与第一个问题解决方法相同。
虽然不知道这样做带来的效果有多明显,或有是否有更好的解决方案,但是可以肯定的是上个版本的代码会大量占用内存并频繁与数据库交互。本人是数据库小白,希望有更好的方案可以提出来 ^_^
如果您觉得阅读本文对您有帮助,请点一下 "推荐" 按钮,您的 "推荐" 将是我最大的写作动力!如果您想持续关注我的文章,请扫描二维码,关注 JackieZheng 的微信公众号,我会将我的文章推送给您,并和您一起分享我日常阅读过的优质文章。
来源: