|
main 方法非常简单。创建一个新 miniHTMLParser 实例并着手处理用户定义的站点(argv[1])和链接(argv[2])。然后获取这个链接的内容,将其传递给 HTML 解析器,并获取下一个要访问的链接(如果存在)。当还存在需要访问的链接时,循环继续。
要调用这个 Web spider,需要提供一个 Web 站点地址和一个链接:
./minispider.py www.fsf.org /
在本例中,会请求 Free Software Foundation 的根文件。这个命令的结果如清单 8 所示。可以看到新链接已经被加入检查队列和那些被忽略的链接中,例如非本地链接。在这个清单底部,可以看到在根文件所找到的审查链接。
清单 8. minispider 脚本的输出结果
[mtj@camus]$ ./minispider.py www.fsf.org /
Checking link /
ignoring hiddenStructure
ignoring http://www.fsf.org
ignoring http://www.fsf.org
ignoring http://www.fsf.org/news
ignoring http://www.fsf.org/events
ignoring http://www.fsf.org/campaigns
ignoring http://www.fsf.org/resources
ignoring http://www.fsf.org/donate
ignoring http://www.fsf.org/associate
ignoring http://www.fsf.org/licensing
ignoring http://www.fsf.org/blogs
ignoring http://www.fsf.org/about
ignoring https://www.fsf.org/login_form
ignoring http://www.fsf.org/join_form
ignoring http://www.fsf.org/news/fs-award-2005.html
ignoring http://www.fsf.org/news/fsfsysadmin.html
ignoring http://www.fsf.org/news/digital-communities.html
ignoring http://www.fsf.org/news/patents-defeated.html
ignoring /news/RSS
ignoring http://www.fsf.org/news
ignoring http://www.fsf.org/blogs/rms/entry-20050802.html
ignoring http://www.fsf.org/blogs/rms/entry-20050712.html
ignoring http://www.fsf.org/blogs/rms/entry-20050601.html
ignoring http://www.fsf.org/blogs/rms/entry-20050526.html
ignoring http://www.fsf.org/blogs/rms/entry-20050513.html
ignoring http://www.fsf.org/index_html/SimpleBlogFullSearch
ignoring documentContent
ignoring http://www.fsf.org/index_html/sendto_form
ignoring javascript:this.print();
adding licensing/essays/free-sw.html
ignoring /licensing/essays
ignoring http://www.gnu.org/philosophy
ignoring http://www.freesoftwaremagazine.com
ignoring donate
ignoring join_form
adding associate/index_html
ignoring http://order.fsf.org
adding donate/patron/index_html
adding campaigns/priority.html
ignoring http://r300.sf.net/
ignoring http://developer.classpath.org/mediation/OpenOffice2GCJ4
ignoring http://gcc.gnu.org/java/index.html
ignoring http://www.gnu.org/software/classpath/
ignoring http://gplflash.sourceforge.net/
ignoring campaigns
adding campaigns/broadcast-flag.html
ignoring http://www.gnu.org
ignoring /fsf/licensing
ignoring http://directory.fsf.org
ignoring http://savannah.gnu.org
ignoring mailto:webmaster@fsf.org
ignoring http://www.fsf.org/Members/root
ignoring http://www.plonesolutions.com
ignoring http://www.enfoldtechnology.com
ignoring http://blacktar.com
ignoring http://plone.org
ignoring http://www.section508.gov
ignoring http://www.w3.org/WAI/WCAG1AA-Conformance
ignoring http://validator.w3.org/check/referer
ignoring http://jigsaw.w3.org/css-validator/check/referer
ignoring http://plone.org/browsersupport
Checking link licensing/essays/free-sw.html
ignoring mailto:webmaster
Checking link associate/index_html
ignoring mailto:webmaster
Checking link donate/patron/index_html
ignoring mailto:webmaster
Checking link campaigns/priority.html
ignoring mailto:webmaster
Checking link campaigns/broadcast-flag.html
ignoring mailto:webmaster
done
[mtj@camus]$
|
这个例子展示了 Web spider 爬行的阶段。当客户机读取一个文件之后,就对这个页面的内容进行扫描,这与索引程序的情况相同。
Linux spider 工具
现在您已经学会怎么样实现 scraper 和 spider 了。有一些 Linux 工具也可以提供类似功能。
wget 命令(代表 Web get 之义)是一个获取 Web 内容的有用工具,它可以递归遍历 Web 站点并从中提取感兴趣的内容。其中 Web 站点、所感兴趣的内容以及其他一些管理选项都可以自定义。这个命令随后就可以将这些文件下载到本地主机上。例如,下面这个命令可以连接到所指定的 URL 上并对其进行递归遍历,不过深度不会超过 3 层,然后会从中提取扩展名为 mp3、mpg、mpeg 或 avi 的内容。
wget -A mp3,mpg,mpeg,avi -r -l 3 http://<some URL>
curl 命令也可以类似地进行操作,其优点是现在它仍然在积极的开发完善之中。可以使用的其他类似命令还有 snarf、fget 和
共6页: 上一页 [1] [2] [3] [4] 5 [6] 下一页
|