TypeError:无法在python中连接'str'和'builtin_function_or_method'对象

时间:2012-03-16 04:06:49

标签: python twitter

请问我是python编码的初学者。这是我的代码,无法解决错误。有人可以告诉我实际代码中的概率是什么。提前谢谢。

import urllib2
username = '<YOUR USERNAME>'
password = '<YOUR PASSWORD>'
format = 'json' # json or xml
filename = 'archive.json' # filename of the archive
tweets = 164 # number of tweets
pages = (int(float(tweets)/float(80)))+1
auth = urllib2.HTTPPasswordMgrWithDefaultRealm()
auth.add_password(None, 'http://twitter.com/account/', username, password)
authHandler = urllib2.HTTPBasicAuthHandler(auth)
opener = urllib2.build_opener(authHandler)
urllib2.install_opener(opener) 
i = 1
response = ''
print 'Downloading tweets. Note that this may take some time'
while i <= pages:
    request = urllib2.Request('http://twitter.com/statuses/user_timeline/account.' \
    + format + '?page=' + str(i))
    response = response + urllib2.urlopen(request).read()
    i = i + 1
handle = open(filename,"w")
handle.write(response)
handle.close()
print 'Archived ' + str(tweets) + ' of ' + username + \
'\'s tweets to ' + filename

错误如下所示:

**Traceback (most recent call last):
  File "<pyshell#14>", line 3, in <module>
    + format + '?page=' + str(i))
TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects**

1 个答案:

答案 0 :(得分:2)

format是一个内置函数。如果您尝试将+与内置format和字符串一起使用,则引用的错误就是您所获得的错误。

您之前的作业format = 'json'应该隐藏了内置函数。但是你的错误跟踪表明你是从某种shell运行它,而不是实际执行你发布它的代码。因此,在不知道究竟是什么执行的情况下,我的猜测是,format的作业无论出于何种原因都无效。