Python 2.7 仅针对给定方法参数的Python urllib.quote失败
我试图学习python,因此编写了一个小的scraper。 我已经走了很远,但是在连接字符串时遇到了问题 我的计划是,只有当它们作为我函数的参数给出时,才添加thinks。代码如下所示:Python 2.7 仅针对给定方法参数的Python urllib.quote失败,python-2.7,urllib,Python 2.7,Urllib,我试图学习python,因此编写了一个小的scraper。 我已经走了很远,但是在连接字符串时遇到了问题 我的计划是,只有当它们作为我函数的参数给出时,才添加thinks。代码如下所示: def buildUrl(self, artist, album=None, title=None): url = self.url + urllib.quote(artist) if title is not None: url = url + '/' + urllib.quote(alb
def buildUrl(self, artist, album=None, title=None):
url = self.url + urllib.quote(artist)
if title is not None: url = url + '/' + urllib.quote(album)
if album is not None: url = url + '/' + urllib.quote(title)
return url
现在问题是我得到了以下错误:
File "C:\Users\lony\workspace-python\scrapper\bs.py", line 22, in buildUrl
if title is not None: url = url + '/' + urllib.quote(album)
File "C:\Program Files (x86)\Python27\lib\urllib.py", line 1269, in quote
raise TypeError('None object cannot be quoted')
TypeError: None object cannot be quoted
我做错了什么?如果您的
条件不匹配:
if title is not None: url = url + '/' + urllib.quote(album)
^^^^^ ^^^^^
天哪,就是这么简单;)谢谢接下来的问题你们知道我为什么要把self作为第一个论点吗。如果我调用这个函数,我的第一个参数是artist,与self没有任何关系,为什么会这样呢?self
是一个隐含参数。它被传递给所有实例方法。Hm。。所以我总是需要它?什么是替代品?self
只是一个标准名称。您可以传入potato
,它会起作用,但是对于每个类方法,您总是需要某种self
参数。