仅针对给定方法参数的Python urllib.quote失败

时间:2013-05-23 22:32:20

标签: python-2.7 urllib

我试着学习python,因此编写了一个小刮刀。 我走得很远,但是在连接字符串方面遇到了问题。

我的计划是只在将我们的函数作为参数给出时添加think。代码如下所示:

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的if条件不匹配:

if title is not None: url = url + '/' + urllib.quote(album)
   ^^^^^                                             ^^^^^