最近,我开始构建一个简单的网络爬虫。我的初始代码只迭代了两次就运行得很好,但是当我试图将它转换成一个具有错误异常处理的类时,它就不再编译了。你知道吗
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)
你有两个问题。一个是这一行:
您没有创建
WebCrawler
的实例,只是将名称myCrawler
绑定到WebCrawler
(基本上,为类创建一个别名)。您应该这样做:然后,在这条线上:
Python实例方法将接收器作为方法的第一个参数。它通常被称为
self
,但从技术上讲,你可以随便叫它什么。因此,您应该将方法签名更改为:(对于定义的其他方法,也需要这样做。)
相关问题 更多 >
编程相关推荐