我正在学习一些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行,它实际上会运行,但是我在保存的文件中得到了这个奇怪的输出

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 - 如何在Twython中修复“AttributeError:'dict'对象没有属性'append'”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48244362/

10-12 20:56