如果我在没有最后一行的情况下运行代码:getVal(tweet['retweeted_status']['favorite_count']),,则抓取有效,但是当我添加此行时,出现错误消息

有人知道我在做什么错吗?

q = "David_Cameron"
results = twitter_user_timeline(twitter_api, q)
print len(results)
# Show one sample search result by slicing the list...
# print json.dumps(results[0], indent=1)
csvfile = open(q + '_timeline.csv', 'w')
csvwriter = csv.writer(csvfile)
csvwriter.writerow(['created_at',
                'user-screen_name',
                'text',
                'coordinates lng',
                'coordinates lat',
                'place',
                'user-location',
                'user-geo_enabled',
                'user-lang',
                'user-time_zone',
                'user-statuses_count',
                'user-followers_count',
                'user-created_at'])
for tweet in results:
    csvwriter.writerow([tweet['created_at'],
                    getVal(tweet['user']['screen_name']),
                    getVal(tweet['text']),
                    getLng(tweet['coordinates']),
                    getLat(tweet['coordinates']),
                    getPlace(tweet['place']),
                    getVal(tweet['user']['location']),
                    getVal(tweet['user']['geo_enabled']),
                    getVal(tweet['user']['lang']),
                    getVal(tweet['user']['time_zone']),
                    getVal(tweet['user']['statuses_count']),
                    getVal(tweet['user']['followers_count']),
                    getVal(tweet['user']['created_at']),
                    getVal(tweet['retweeted_status']['favorite_count']),
                    ])
print "done"

最佳答案

根据API在https://dev.twitter.com/overview/api/tweets处,此属性可能存在或不存在。

如果它不存在,则将无法访问该属性。您可以使用in运算符进行安全查找,方法是先检查是否存在

retweeted_favourite_count = tweet['retweeted_status']['favourite_count'] if 'retweeted_status' in tweet else None

或采取假设它在那里但在不存在时进行处理的方式

try: retweeted_favourite_count = tweet['retweeted_status']['favourite_count']except KeyError: retweeted_favourite_count = 0

然后在写行功能中分配retweeted_favourite_count值。

另外,您的CSV标头行缺少对转发的收藏计数的描述

更新的示例:
for tweet in results: #Notice this is one long line not two rows. retweeted_favourite_count = tweet['retweeted_status']['favourite_count'] if 'retweeted_status' in tweet else None csvwriter.writerow([tweet['created_at'], getVal(tweet['user']['screen_name']), getVal(tweet['text']), getLng(tweet['coordinates']), getLat(tweet['coordinates']), getPlace(tweet['place']), getVal(tweet['user']['location']), getVal(tweet['user']['geo_enabled']), getVal(tweet['user']['lang']), getVal(tweet['user']['time_zone']), getVal(tweet['user']['statuses_count']), getVal(tweet['user']['followers_count']), getVal(tweet['user']['created_at']), # And insert it here instead getVal(retweeted_favourite_count), ])

您也可以切换线路:

getVal(tweet['retweeted_status']['favorite_count'])

正如帕德里亚克·坎宁安建议的

getVal(tweet.get('retweeted_status', {}).get('favourite_count', None)

09-08 10:04