How can I fix "AttributeError: 'dict' object has no attribute 'append'" in Twython
How can I fix "AttributeError: 'dict' object has no attribute 'append'" in Twython
我正在学习一些 python,我想编写一个脚本,从我的 Twitter 中提取我的关注者用户名并将其保存到一个文件中。
脚本来自本站https://www.silkstream.net/blog/2014/06/playing-with-followers-with-twython-csv.html
下面是我输入的代码
from twython import Twython
import datetime
app_key = ""
app_secret = ""
oauth_token = ""
oauth_token_secret = ""
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
#twitter.update_status(status = "Testing a script dont mind this tweet")
followers = []
datestamp = datetime.datetime.now().strftime("%Y-%m-%d")
username = input("what is your username: ")
next_cursor = -1
while(next_cursor):
get_followers = twitter.get_followers_list(screen_name=username,count=200,cursor=next_cursor)
for followers in get_followers["users"]:
followers.append(follower["screen_name"].encode("utf-8"))
next_cursor = get_followers["next_cursor"]
followers_text = open(username+"-"+datestamp+".txt","a")
followers_text.write("%s has %s followers (%s)):" % (str(username),str(len(followers)),str(datestamp))+"".join(followers))
followers_text.close()
当我 运行 我得到这个输出的程序时
File "first.py", line 19, in <module>
followers.append(follower["screen_name"].encode("utf-8"))
AttributeError: 'dict' object has no attribute 'append'
如果我注释掉第 19 行,它实际上 运行s 但是我在保存的文件中得到了这个奇怪的输出
idid_strnamescreen_namelocationdescriptionurlentitiesprotectedfollowers_countfriends_countlisted_countcreated_atfavourites_countutc_offsettime_zonegeo_enabledverifiedstatuses_countlangstatuscontributors_enabledis_translatoris_translation_enabledprofile_background_colorprofile_background_image_urlprofile_background_image_url_httpsprofile_background_tileprofile_image_urlprofile_image_url_httpsprofile_banner_urlprofile_link_colorprofile_sidebar_border_colorprofile_sidebar_fill_colorprofile_text_colorprofile_use_background_imagehas_extended_profiledefault_profiledefault_profile_imagefollowinglive_followingfollow_request_sentnotificationsmutingblockingblocked_bytranslator_type
看起来它认为某物是一本字典(或者至少这是我的猜测)但我没有字典,对吗?
您将名称 followers
用作变量名称两次,您应该使用两个不同的名称:
followers = []
# ...
for followers in get_followers["users"]:
我正在学习一些 python,我想编写一个脚本,从我的 Twitter 中提取我的关注者用户名并将其保存到一个文件中。
脚本来自本站https://www.silkstream.net/blog/2014/06/playing-with-followers-with-twython-csv.html
下面是我输入的代码
from twython import Twython
import datetime
app_key = ""
app_secret = ""
oauth_token = ""
oauth_token_secret = ""
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
#twitter.update_status(status = "Testing a script dont mind this tweet")
followers = []
datestamp = datetime.datetime.now().strftime("%Y-%m-%d")
username = input("what is your username: ")
next_cursor = -1
while(next_cursor):
get_followers = twitter.get_followers_list(screen_name=username,count=200,cursor=next_cursor)
for followers in get_followers["users"]:
followers.append(follower["screen_name"].encode("utf-8"))
next_cursor = get_followers["next_cursor"]
followers_text = open(username+"-"+datestamp+".txt","a")
followers_text.write("%s has %s followers (%s)):" % (str(username),str(len(followers)),str(datestamp))+"".join(followers))
followers_text.close()
当我 运行 我得到这个输出的程序时
File "first.py", line 19, in <module>
followers.append(follower["screen_name"].encode("utf-8"))
AttributeError: 'dict' object has no attribute 'append'
如果我注释掉第 19 行,它实际上 运行s 但是我在保存的文件中得到了这个奇怪的输出
idid_strnamescreen_namelocationdescriptionurlentitiesprotectedfollowers_countfriends_countlisted_countcreated_atfavourites_countutc_offsettime_zonegeo_enabledverifiedstatuses_countlangstatuscontributors_enabledis_translatoris_translation_enabledprofile_background_colorprofile_background_image_urlprofile_background_image_url_httpsprofile_background_tileprofile_image_urlprofile_image_url_httpsprofile_banner_urlprofile_link_colorprofile_sidebar_border_colorprofile_sidebar_fill_colorprofile_text_colorprofile_use_background_imagehas_extended_profiledefault_profiledefault_profile_imagefollowinglive_followingfollow_request_sentnotificationsmutingblockingblocked_bytranslator_type
看起来它认为某物是一本字典(或者至少这是我的猜测)但我没有字典,对吗?
您将名称 followers
用作变量名称两次,您应该使用两个不同的名称:
followers = []
# ...
for followers in get_followers["users"]: