现在的位置: 首页 > 综合 > 正文

网络爬虫(Spider)Java实现原理

2014年11月20日 ⁄ 综合 ⁄ 共 15912字 ⁄ 字号 评论关闭

“网络蜘蛛”或者说“网络爬虫”,是一种能访问网站并跟踪链接的程序,通过它,可快速地画出一个网站所包含的网页地图信息。本文主要讲述如何使用Java编程来构建一个“蜘蛛”,我们会先以一个可复用的蜘蛛类包装一个基本的“蜘蛛”,并在示例程序中演示如何创建一个特定的“蜘蛛”来扫描相关网站并找出死链接。
Java语言在此非常适合构建一个“蜘蛛”程序,其内建了对HTTP协议的支持,通过它可以传输大部分的网页信息;其还内建了一个HTML解析器,正是这两个原因使Java语言成为本文构建“蜘蛛”程序的首选。
文章后面例1的示例程序,将会扫描一个网站,并寻找死链接。使用这个程序时需先输入一个URL并单击“Begin”按钮,程序开始之后,“Begin”按钮会变成“Cancel”按钮。在程序扫描网站期间,会在“Cancel”按钮之下显示进度,且在检查当前网页时,也会显示相关正常链接与死链接的数目,死链接将显示在程序底部的滚动文本框中。单击“Cancel”按钮会停止扫描过程,之后可以输入一个新的URL;如果期间没有单击“Cancel”,程序将会一直运行直到查找完所有网页,此后,“Cancel”按钮会再次变回“Begin”,表示程序已停止。
下面将演示示例程序是如何与可复用“Spider”类交互的,示例程序包含在例1的CheckLinks类中,这个类实现了ISpiderReportable接口,如例2所示,正是通过这个接口,蜘蛛类才能与示例程序相交互。在这个接口中,定义了三个方法:第一个方法是“spiderFoundURL”,它在每次程序定位一个URL时被调用,如果方法返回true,表示程序应继续执行下去并找出其中的链接;第二个方法是“spiderURLError”,它在每次程序检测URL导致错误时被调用(如“404
页面未找到”);第三个方法是“spiderFoundEMail”,它在每次发现电子邮件地址时被调用。有了这三个方法,Spider类就能把相关信息反馈给创建它的程序了。

在begin方法被调用后,“蜘蛛”就开始工作了;为允许程序重绘其用户界面,“蜘蛛”是作为一个单独的线程启动的。点击“Begin”按钮会开始这个后台线程,当后台线程运行之后,又会调用“CheckLinks”类的run方法,而run方法是由Spider对象实例化时启动的,如下所示:
spider = new Spider(this); 
spider.clear(); 
base = new URL(url.getText()); 
spider.addURL(base); 
spider.begin(); 
首先,一个新的Spider对象被实例化,在此,需要传递一个“ISpiderReportable”对象给Spider对象的构造函数,因为“CheckLinks”类实现了“ISpiderReportable”接口,只需简单地把它作为当前对象(可由关键字this表示)传递给构造函数即可;其次,在程序中维护了一个其访问过的URL列表,而“clear”方法的调用则是为了确保程序开始时URL列表为空,程序开始运行之前必须添加一个URL到它的待处理列表中,此时用户输入的URL则是添加到列表中的第一个,程序就由扫描这个网页开始,并找到与这个起始URL相链接的其他页面;最后,调用“begin”方法开始运行“蜘蛛”,这个方法直到“蜘蛛”工作完毕或用户取消才会返回。
当“蜘蛛”运行时,可以调用由“ISpiderReportable”接口实现的三个方法来报告程序当前状态,程序的大部分工作都是由“spiderFoundURL”方法来完成的,当“蜘蛛”发现一个新的URL时,它首先检查其是否有效,如果这个URL导致一个错误,就会把它当作一个死链接;如果链接有效,就会继续检查它是否在一个不同的服务器上,如果链接在同一服务器上,“spiderFoundURL”返回true,表示“蜘蛛”应继续跟踪这个URL并找出其他链接,如果链接在另外的服务器上,就不会扫描是否还有其他链接,因为这会导致“蜘蛛”不断地浏览Internet,寻找更多、更多的网站,所以,示例程序只会查找用户指定网站上的链接。
构造Spider类
前面已经讲了如何使用Spider类,请看例3中的代码。使用Spider类及“ISpiderReportable”接口能方便地为某一程序添加“蜘蛛”功能,下面继续讲解Spider类是怎样工作的。
Spider类必须保持对其访问过的URL的跟踪,这样做的目的是为了确保“蜘蛛”不会访问同一URL一次以上;进一步来说,“蜘蛛”必须把URL分成三组,第一组存储在“workloadWaiting”属性中,包含了一个未处理的URL列表,“蜘蛛”要访问的第一个URL也存在其中;第二组存储在“workloadProcessed”中,它是“蜘蛛”已经处理过且无需再次访问的URL;第三组存储在“workloadError”中,包含了发生错误的URL。
Begin方法包含了Spider类的主循环,其一直重复遍历“workloadWaiting”,并处理其中的每一个页面,当然我们也想到了,在这些页面被处理时,很可能有其他的URL添加到“workloadWaiting”中,所以,begin方法一直继续此过程,直到调用Spider类的cancel方法,或“workloadWaiting”中已不再剩有URL。这个过程如下:
cancel = false; 
while ( !getWorkloadWaiting().isEmpty() && !cancel ) {
Object list[] = getWorkloadWaiting().toArray(); 
for ( int i=0; (i
processURL((URL)list[i]); 
}
当上述代码遍历“workloadWaiting”时,它把每个需处理的URL都传递给“processURL”方法,而这个方法才是真正读取并解析URL中HTML信息的。
读取并解析HTML
Java同时支持访问URL内容及解析HTML,而这正是“processURL”方法要做的。在Java中读取URL内容相对还比较简单,下面就是“processURL”方法实现此功能的代码:
URLConnection connection = url.openConnection(); 
if ( (connection.getContentType()!=null) &&!connection.getContentType().toLowerCase().startsWith("text/") ) {
getWorkloadWaiting().remove(url); 
getWorkloadProcessed().add(url); 
log("Not processing because content type is: " +
connection.getContentType() ); 
return; 
}

首先,为每个传递进来的变量url中存储的URL构造一个“URLConnection”对象,因为网站上会有多种类型的文档,而“蜘蛛”只对那些包含HTML,尤其是基于文本的文档感兴趣。前述代码是为了确保文档内容以“text/”打头,如果文档类型为非文本,会从等待区移除此URL,并把它添加到已处理区,这也是为了保证不会再次访问此URL。在对特定URL建立连接之后,接下来就要解析其内容了。下面的代码打开了URL连接,并读取内容:

InputStream is = connection.getInputStream();

Reader r = new InputStreamReader(is);

现在,我们有了一个Reader对象,可以用它来读取此URL的内容,对本文中的“蜘蛛”来说,只需简单地把其内容传递给HTML解析器就可以了。本例中使用的HTML解析器为Swing HTML解析器,其由Java内置,但由于Java对HTML解析的支持力度不够,所以必须重载一个类来实现对HTML解析器的访问,这就是为什么我们要调用“HTMLEditorKit”类中的“getParser”方法。但不幸的是,Sun公司把这个方法置为protected,唯一的解决办法就是创建自己的类并重载“getParser”方法,并把它置为public,这由“HTMLParse”类来实现,请看例4:

import javax.swing.text.html.*;

public class HTMLParse extends HTMLEditorKit {

public HTMLEditorKit.Parser getParser()

{

return super.getParser();

}

}

这个类用在Spider类的“processURL”方法中,我们也会看到,Reader对象会用于读取传递到“HTMLEditorKit.Parser”中网页的内容:

HTMLEditorKit.Parser parse = new HTMLParse().getParser();

parse.parse(r,new Parser(url),true);

请留意,这里又构造了一个新的Parser类,这个Parser类是一个Spider类中的内嵌类,而且还是一个回调类,它包含了对应于每种HTML tag将要调用的特定方法。在本文中,我们只需关心两类回调函数,它们分别对应一个简单tag(即不带结束tag的tag,如

)和一个开始tag,这两类回调函数名为“handleSimpleTag”和“handleStartTag”。因为每种的处理过程都是一样的,所以“handleStartTag”方法仅是简单地调用“handleSimpleTag”,而“handleSimpleTag”则会负责从文档中取出超链接,这些超链接将会用于定位“蜘蛛”要访问的其他页面。在当前tag被解析时,“handleSimpleTag”会检查是否存在一个“href”或超文本引用:

String href = (String)a.getAttribute(HTML.Attribute.HREF);

if( (href==null) && (t==HTML.Tag.FRAME) )

href = (String)a.getAttribute(HTML.Attribute.SRC);

if ( href==null )

return;

如果不存在“href”属性,会继续检查当前tag是否为一个Frame,Frame会使用一个“src”属性指向其他页面,一个典型的超链接通常为以下形式:

上面链接中的“href”属性指向其链接到的页面,但是“linkedpage.html”不是一个地址,它只是指定了这个Web服务器上一个页面上的某处,这称为相对URL,相对URL必须被解析为绝对URL,而这由以下代码完成:

URL url = new URL(base,str);

这又会构造一个URL,str为相对URL,base为这个URL上的页面,这种形式的URL类构造函数可构造一个绝对URL。在URL变为正确的绝对形式之后,通过检查它是否在等待区,来确认此URL是否已经被处理过。如果此URL没有被处理过,它会添加到等待区,之后,它会像其他URL一样被处理。

相关的代码如下所示:

1.CheckLinks.java

import java.awt.*;
       import javax.swing.*;
       import java.net.*;
       import java.io.*;
public class CheckLinks extends javax.swing.JFrame implements
             Runnable,ISpiderReportable {

  /**
   * The constructor. Perform setup here.
   */
  public CheckLinks()
  {
    //{{INIT_CONTROLS
    setTitle("Find Broken Links");
    getContentPane().setLayout(null);
    setSize(405,288);
    setVisible(true);
    label1.setText("Enter a URL:");
    getContentPane().add(label1);
    label1.setBounds(12,12,84,12);
    begin.setText("Begin");
    begin.setActionCommand("Begin");
    getContentPane().add(begin);
    begin.setBounds(12,36,84,24);
    getContentPane().add(url);
    url.setBounds(108,36,288,24);
    errorScroll.setAutoscrolls(true);
    errorScroll.setHorizontalScrollBarPolicy(javax.swing.
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    errorScroll.setVerticalScrollBarPolicy(javax.swing.
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    errorScroll.setOpaque(true);
    getContentPane().add(errorScroll);
    errorScroll.setBounds(12,120,384,156);
    errors.setEditable(false);
    errorScroll.getViewport().add(errors);
    errors.setBounds(0,0,366,138);
    current.setText("Currently Processing: ");
    getContentPane().add(current);
    current.setBounds(12,72,384,12);
    goodLinksLabel.setText("Good Links: 0");
    getContentPane().add(goodLinksLabel);
    goodLinksLabel.setBounds(12,96,192,12);
    badLinksLabel.setText("Bad Links: 0");
    getContentPane().add(badLinksLabel);
    badLinksLabel.setBounds(216,96,96,12);
    //}}
  //{{INIT_MENUS
    //}}

    //{{REGISTER_LISTENERS
    SymAction lSymAction = new SymAction();
    begin.addActionListener(lSymAction);
    //}}
  }

  /**
   * Main method for the application
   * 
   * @param args Not used
   */
  static public void main(String args[])
  {
    (new CheckLinks()).setVisible(true);
  }

  /**
   * Add notifications.
   */
  public void addNotify()
  {
    // Record the size of the window prior to calling parent's
    // addNotify.
    Dimension size = getSize();

super.addNotify();

    if ( frameSizeAdjusted )
      return;
    frameSizeAdjusted = true;

// Adjust size of frame according to the insets and menu bar
    Insets insets = getInsets();
    javax.swing.JMenuBar menuBar = getRootPane().getJMenuBar();
    int menuBarHeight = 0;
    if ( menuBar != null )
      menuBarHeight = menuBar.getPreferredSize().height;
    setSize(insets.left + insets.right + size.width, insets.top +
                          insets.bottom + size.height + 
                          menuBarHeight);
  }

  // Used by addNotify
  boolean frameSizeAdjusted = false;

  //{{DECLARE_CONTROLS
  javax.swing.JLabel label1 = new javax.swing.JLabel();

  /**
   * The begin or cancel button
   */
  javax.swing.JButton begin = new javax.swing.JButton();

  /**
   * The URL being processed
   */
  javax.swing.JTextField url = new javax.swing.JTextField();

  /**
   * Scroll the errors.
   */
  javax.swing.JScrollPane errorScroll =
        new javax.swing.JScrollPane();

  /**
   * A place to store the errors created
   */
  javax.swing.JTextArea errors = new javax.swing.JTextArea();
  javax.swing.JLabel current = new javax.swing.JLabel();
  javax.swing.JLabel goodLinksLabel = new javax.swing.JLabel();
  javax.swing.JLabel badLinksLabel = new javax.swing.JLabel();
  //}}

  //{{DECLARE_MENUS
  //}}

  /**
   * The background spider thread
   */
  protected Thread backgroundThread;

  /**
   * The spider object being used
   */
  protected Spider spider;

  /**
   * The URL that the spider began with
   */
  protected URL base;

  /**
   * How many bad links have been found
   */
  protected int badLinksCount = 0;

  /**
   * How many good links have been found
   */
  protected int goodLinksCount = 0;

  /**
   * Internal class used to dispatch events
   * 
   * @author wuhailin
   * @version 1.0
   */
  class SymAction implements java.awt.event.ActionListener {
    public void actionPerformed(java.awt.event.ActionEvent event)
    {
      Object object = event.getSource();
      if ( object == begin )
        begin_actionPerformed(event);
    }
  }

  /**
   * Called when the begin or cancel buttons are clicked
   * 
   * @param event The event associated with the button.
   */
  void begin_actionPerformed(java.awt.event.ActionEvent event)
  {
    if ( backgroundThread==null ) {
      begin.setLabel("Cancel");
      backgroundThread = new Thread(this);
      backgroundThread.start();
      goodLinksCount=0;
      badLinksCount=0;
    } else {
      spider.cancel();
    }

  }

  /**
   * Perform the background thread operation. This method
   * actually starts the background thread.
   */
  public void run()
  {
    try {
      errors.setText("");
      spider = new Spider(this);
      spider.clear();
      base = new URL(url.getText());
      spider.addURL(base);
      spider.begin();
      Runnable doLater = new Runnable()
      {
        public void run()
        {
          begin.setText("Begin");
        }
      };
      SwingUtilities.invokeLater(doLater);
      backgroundThread=null;

    } catch ( MalformedURLException e ) {
      UpdateErrors err = new UpdateErrors();
      err.msg = "Bad address.";
      SwingUtilities.invokeLater(err);

    }
  }

  /**
   * Called by the spider when a URL is found. It is here
   * that links are validated.
   * 
   * @param base The page that the link was found on.
   * @param url The actual link address.
   */
  public boolean spiderFoundURL(URL base,URL url)
  {
    UpdateCurrentStats cs = new UpdateCurrentStats();
    cs.msg = url.toString();
    SwingUtilities.invokeLater(cs);

    if ( !checkLink(url) ) {
      UpdateErrors err = new UpdateErrors();
      err.msg = url+"(on page " + base + ")/n";
      SwingUtilities.invokeLater(err);
      badLinksCount++;
      return false;
    }

    goodLinksCount++;
    if ( !url.getHost().equalsIgnoreCase(base.getHost()) )
      return false;
    else
      return true;
  }

  /**
   * Called when a URL error is found
   * 
   * @param url The URL that resulted in an error.
   */
  public void spiderURLError(URL url)
  {
  }

  /**
   * Called internally to check whether a link is good
   * 
   * @param url The link that is being checked.
   * @return True if the link was good, false otherwise.
   */
  protected boolean checkLink(URL url)
  {
    try {
      URLConnection connection = url.openConnection();
      connection.connect();
      return true;
    } catch ( IOException e ) {
      return false;
    }
  }

  /**
   * Called when the spider finds an e-mail address
   * 
   * @param email The email address the spider found.
   */
  public void spiderFoundEMail(String email)
  {
  }
  /**
   * Internal class used to update the error information
   * in a Thread-Safe way
    */

class UpdateErrors implements Runnable {
    public String msg;
    public void run()
    {
      errors.append(msg);
    }
  }
  /**
   * Used to update the current status information
   * in a "Thread-Safe" way
   */

  class UpdateCurrentStats implements Runnable {
    public String msg;
    public void run()
    {
      current.setText("Currently Processing: " + msg );
      goodLinksLabel.setText("Good Links: " + goodLinksCount);
      badLinksLabel.setText("Bad Links: " + badLinksCount);
    }
  }
}

2.ISpiderReportable .java

import java.net.*;

interface ISpiderReportable {
  public boolean spiderFoundURL(URL base,URL url);
  public void spiderURLError(URL url);
  public void spiderFoundEMail(String email);
}

3.Spider .java

import java.util.*;
       import java.net.*;
       import java.io.*;
       import javax.swing.text.*;
       import javax.swing.text.html.*;

/**
 * That class implements a reusable spider
 */
public class Spider {

  /**
   * A collection of URLs that resulted in an error
   */
  protected Collection workloadError = new ArrayList(3);

  /**
   * A collection of URLs that are waiting to be processed
   */
  protected Collection workloadWaiting = new ArrayList(3);

  /**
   * A collection of URLs that were processed
   */
  protected Collection workloadProcessed = new ArrayList(3);

  /**
   * The class that the spider should report its URLs to
   */
  protected ISpiderReportable report;

  /**
   * A flag that indicates whether this process
   * should be canceled
   */
  protected boolean cancel = false;

  /**
   * The constructor
   * 
   * @param report A class that implements the ISpiderReportable
   * interface, that will receive information that the
   * spider finds.
   */
  public Spider(ISpiderReportable report)
  {
    this.report = report;
  }

  /**
   * Get the URLs that resulted in an error.
   * 
   * @return A collection of URL's.
   */
  public Collection getWorkloadError()
  {
    return workloadError;
  }

  /**
   * Get the URLs that were waiting to be processed.
   * You should add one URL to this collection to
   * begin the spider.
   * 
   * @return A collection of URLs.
   */
  public Collection getWorkloadWaiting()
  {
    return workloadWaiting;
  }

  /**
   * Get the URLs that were processed by this spider.
   * 
   * @return A collection of URLs.
   */
  public Collection getWorkloadProcessed()
  {
    return workloadProcessed;
  }   

  /**
   * Clear all of the workloads.
   */
  public void clear()
  {
    getWorkloadError().clear();
    getWorkloadWaiting().clear();
    getWorkloadProcessed().clear();
  }

  /**
   * Set a flag that will cause the begin
   * method to return before it is done.
   */
  public void cancel()
  {
    cancel = true;
  }

  /**
   * Add a URL for processing.
   * 
   * @param url
   */
  public void addURL(URL url)
  {
    if ( getWorkloadWaiting().contains(url) )
      return;
    if ( getWorkloadError().contains(url) )
      return;
    if ( getWorkloadProcessed().contains(url) )
      return;
    log("Adding to workload: " + url );
    getWorkloadWaiting().add(url);
  }

  /**
   * Called internally to process a URL
   * 
   * @param url The URL to be processed.
   */
  public void processURL(URL url)
  {
    try {
      log("Processing: " + url );
      // get the URL's contents
      URLConnection connection = url.openConnection();
      if ( (connection.getContentType()!=null) &&
           !connection.getContentType().toLowerCase().startsWith("text/") ) {
        getWorkloadWaiting().remove(url);
        getWorkloadProcessed().add(url);
        log("Not processing because content type is: " +
             connection.getContentType() );
        return;
      }
      
      // read the URL
      InputStream is = connection.getInputStream();
      Reader r = new InputStreamReader(is);
      // parse the URL
      HTMLEditorKit.Parser parse = new HTMLParse().getParser();
      parse.parse(r,new Parser(url),true);
    } catch ( IOException e ) {
      getWorkloadWaiting().remove(url);
      getWorkloadError().add(url);
      log("Error: " + url );
      report.spiderURLError(url);
      return;
    }
    // mark URL as complete
    getWorkloadWaiting().remove(url);
    getWorkloadProcessed().add(url);
    log("Complete: " + url );

  }

  /**
   * Called to start the spider
   */
  public void begin()
  {
    cancel = false;
    while ( !getWorkloadWaiting().isEmpty() && !cancel ) {
      Object list[] = getWorkloadWaiting().toArray();
      for ( int i=0;(i<list.length)&&!cancel;i++ )
        processURL((URL)list[i]);
    }
  }

/**
 * A HTML parser callback used by this class to detect links
 * 
 * @author wuhailin
 * @version 1.0
 */
  protected class Parser
  extends HTMLEditorKit.ParserCallback {
    protected URL base;

    public Parser(URL base)
    {
      this.base = base;
    }

    public void handleSimpleTag(HTML.Tag t,
                                MutableAttributeSet a,int pos)
    {
      String href = (String)a.getAttribute(HTML.Attribute.HREF);
      
      if( (href==null) && (t==HTML.Tag.FRAME) )
        href = (String)a.getAttribute(HTML.Attribute.SRC);
        
      if ( href==null )
        return;

      int i = href.indexOf('#');
      if ( i!=-1 )
        href = href.substring(0,i);

      if ( href.toLowerCase().startsWith("mailto:") ) {
        report.spiderFoundEMail(href);
        return;
      }

      handleLink(base,href);
    }

    public void handleStartTag(HTML.Tag t,
                               MutableAttributeSet a,int pos)
    {
      handleSimpleTag(t,a,pos);    // handle the same way

    }

    protected void handleLink(URL base,String str)
    {
      try {
        URL url = new URL(base,str);
        if ( report.spiderFoundURL(base,url) )
          addURL(url);
      } catch ( MalformedURLException e ) {
        log("Found malformed URL: " + str );
      }
    }
  }

  /**
   * Called internally to log information
   * This basic method just writes the log
   * out to the stdout.
   * 
   * @param entry The information to be written to the log.
   */
  public void log(String entry)
  {
    System.out.println( (new Date()) + ":" + entry );
  }
}

4.HTMLParse .java

import javax.swing.text.html.*;

public class HTMLParse extends HTMLEditorKit {

  public HTMLEditorKit.Parser getParser()
  {
    return super.getParser();
  }
}

抱歉!评论已关闭.