Getting Started Spidering a Site使用Chilkat(python)练习的一个爬虫(from :http://www.example-code.com)


英文原文地址:
http://www.example-code.com/python/spider_begin.asp

一、下载:上面页面中的 Download Chilkat Python Library(Chilkat :翻译出来的意思是奇尔卡特人(北美阿拉斯加印第安部落特林基特人的分支),老美还是蛮有意思的,啥时候咱们中国人做的软件也起一个叫啥“纳西”或是“蒙古”库的软件;那样是不是感觉很cool?,好了,不多说着了,有点跑题了。)
二、安装
解压之后有一个QuickStart的页面;里面说要安装 Python 2.5,你要确认一下你的python,必须是2.5;
而且还有一点提醒一下(因为我是python菜鸟,本人对同样是菜鸟的得有必要提醒一下)就是:在QuickStart的页面的页面里说
you only need to add a __path__ = ["dir_with_chilkat_pyd"],这个 "dir_with_chilkat_pyd"和java中是一样的处理方式,不是“/”而是“/”(目录间隔符号);然后你可以在__init__.py中加上“__path__”;这样你就可以使用Chilkat 了;
三、使用
源码:
  1. from extra import  chilkat
  2. #  The Chilkat Spider component/library is free.
  3. spider = chilkat.CkSpider()
  4. #  The spider object crawls a single web site at a time.  As you'll see
  5. #  in later examples, you can collect outbound links and use them to
  6. #  crawl the web.  For now, we'll simply spider 10 pages of chilkatsoft.com
  7. spider.Initialize("www.chilkatsoft.com")
  8. #  Add the 1st URL:
  9. spider.AddUnspidered("http://www.chilkatsoft.com/")
  10. #  Begin crawling the site by calling CrawlNext repeatedly.
  11. for i in range(0,10):
  12.     success = spider.CrawlNext()
  13.     if (success == True):
  14.         #  Show the URL of the page just spidered.
  15.         print spider.lastUrl()
  16.         #  The HTML is available in the LastHtml property
  17.     else:
  18.         #  Did we get an error or are there no more URLs to crawl?
  19.         if (spider.get_NumUnspidered() == 0):
  20.             print "No more URLs to spider"
  21.         else:
  22.             print spider.lastErrorText()
  23.     #  Sleep 1 second before spidering the next URL.
  24.     spider.SleepMs(1000)

注:我在这里把Chilkat放在了包 extra中调用;

四、代码说明

  1. spider.AddUnspidered("http://www.chilkatsoft.com/") 可以说是一种定义种子url
  2. 代码很简单就是两个if else,就是判断是否爬到页面和输出出错信息,最后定义spider休息时间,我还没看源代码,应该是多线程的了。
总结说明:这是一个很简单的爬虫使用,当然一个有用的爬虫肯定是不止这些的,但是 chilkat也提供了一些基本概念的实现,如下所示,
当然 chilkat还有很多其他有用的功能,

The full suite of Chilkat components are now available for the Python scripting language. Commercially licensed components include:

  1. Email (POP3 / SMTP)
  2. IMAP
  3. Zip, GZip, and Unix Compress
  4. Encryption
  5. RSA
  6. MIME and S/MIME
  7. FTP
  8. HTTP
  9. HTML-to-XML
  10. Charset
  11. Bounce

上面的11个是要收费的产品

下面的4个免费

Freeware components include:

  1. XML
  2. Digital Certificates
  3. Spider
  4. Upload













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值