0

我尝试学习 python,因此编写了一个小爬虫程序。我已经走了很远,但是在连接字符串时遇到了问题。

我的计划是仅在将想法作为参数提供给我的函数时才添加想法。代码如下所示:

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

我做错了什么?

4

1 回答 1

0

您的if条件不匹配:

if title is not None: url = url + '/' + urllib.quote(album)
   ^^^^^                                             ^^^^^
于 2013-05-23T22:36:44.043 回答