Web爬虫类无法正常工作

时间:2015-01-28 20:31:28

标签: python web web-crawler

最近,我开始构建一个简单的Web爬虫。我刚刚迭代两次的初始代码完美无缺,但是当我尝试将其转换为具有错误异常处理的类时,它不再编译。

import re, urllib
class WebCrawler:
    """A Simple Web Crawler That Is Readily Extensible"""
    def __init__():
        size = 1
    def containsAny(seq, aset):
        for c in seq:
            if c in aset: return True
        return False

    def crawlUrls(url, depth):
        textfile = file('UrlMap.txt', 'wt')
        urlList = [url]
        size = 1
        for i in range(depth):
            for ee in range(size):
                if containsAny(urlList[ee], "http://"):
                    try:
                        webpage = urllib.urlopen(urlList[ee]).read()
                        break
                    except:
                        print "Following URL failed!"
                        print urlList[ee]
                    for ee in re.findall('''href=["'](.[^"']+)["']''',webpage, re.I):
                        print ee
                        urlList.append(ee)
                        size+=1
                        textfile.write(ee+'\n')

myCrawler = WebCrawler

myCrawler.crawlUrls("http://www.wordsmakeworlds.com/", 2)

这是生成的错误代码。

Traceback (most recent call last):
  File "C:/Users/Noah Huber-Feely/Desktop/Python/WebCrawlerClass", line 33, in <module>
    myCrawler.crawlUrls("http://www.wordsmakeworlds.com/", 2)
TypeError: unbound method crawlUrls() must be called with WebCrawler instance as first argument (got str instance instead)

1 个答案:

答案 0 :(得分:1)

你有两个问题。一个是这一行:

myCrawler = WebCrawler

您没有创建WebCrawler的实例,只是将名称myCrawler绑定到WebCrawler(基本上,为类创建别名)。你应该这样做:

myCrawler = WebCrawler()

然后,在这一行:

def crawlUrls(url, depth):

Python实例方法将接收器作为方法的第一个参数。它通常被称为self,但从技术上讲,您可以随意调用它。因此,您应该将方法签名更改为:

def crawlUrls(self, url, depth):

(您还需要为您定义的其他方法执行此操作。)