TypeError:无法连接python中的“str”和“builtin”函数或“u method”对象

2024-04-26 06:16:36 发布

您现在位置:Python中文网/ 问答频道 /正文

拜托,我是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

错误如下:

^{pr2}$

Tags: of代码authjsonformatyourresponse错误
1条回答
网友
1楼 · 发布于 2024-04-26 06:16:36

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

您先前的任务format = 'json'应该已经隐藏了内置函数。但是错误跟踪表明您是从某种shell运行的,而不是实际执行您发布的代码。因此,在不知道具体执行什么的情况下,我猜您对format的赋值不管出于什么原因都没有生效。在

相关问题 更多 >