Twitter数据到csv,尝试添加到CSV文件时收到错误

时间:2016-08-10 18:49:24

标签: python csv twitter tweepy

尝试将最近24小时的数据放入CSV文件并使用tweepy for python

Traceback (most recent call last):
File "**", line 74, in <module>
get_all_tweets("BQ")
File "**", line 66, in get_all_tweets
writer.writerows(outtweets)
File "C:\Users\Barry\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 6-7: character maps to <undefined>

作为一个错误,任何人都可以看到错误,因为这在今天之前以某种身份运作。

代码:     def get_all_tweets(screen_name):

# authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)

# initialize a list to hold all the tweepy Tweets
alltweets = []    

# make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.home_timeline (screen_name=screen_name, count=200)

# save most recent tweets
alltweets.extend(new_tweets)

# save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

outtweets = []

page = 1
deadend = False


print ("getting tweets before %s" % (oldest))

# all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.home_timeline(screen_name=screen_name, count=200, max_id=oldest, page=page)

# save most recent tweets
alltweets.extend(new_tweets)

# update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

print ("...%s tweets downloaded so far" % (len(alltweets)))

for tweet in alltweets:

    if (datetime.datetime.now() - tweet.created_at).days < 1:
        # transform the tweepy tweets into a 2D array that will populate the csv    
        outtweets.append([tweet.user.name, tweet.created_at, tweet.text.encode("utf-8")])

    else:
        deadend = True
        return
    if not deadend:
        page += 1

# write the csv    
with open('%s_tweets.csv' % screen_name, 'w') as f:
    writer = csv.writer(f)
    writer.writerow(["name", "created_at", "text"])
    writer.writerows(outtweets)
pass


print ("CSV written")

if __name__ == '__main__':
# pass in the username of the account you want to download
get_all_tweets("BQ")

**编辑1 **

 with open('%s_tweets.csv' % screen_name, 'w', encode('utf-8')) as f:
 TypeError: an integer is required (got type bytes)

**编辑2 **

 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
 UnicodeEncodeError: 'charmap' codec can't encode characters in position 6-7: character maps to <undefined>

2 个答案:

答案 0 :(得分:1)

您的问题在于某些推文中的字符。您无法将其写入您打开的文件。 如果你替换这一行

with open('%s_tweets.csv' % screen_name, 'w') as f:

用这个:

with open('%s_tweets.csv' % screen_name, mode='w', encoding='utf-8') as f:

它应该有用。请注意,这只适用于python 3.x

答案 1 :(得分:0)

似乎该角色无法编码为utf-8。虽然查看触发错误的相关推文可能很有用,但您可以通过将tweet.text.encode("utf-8")更改为tweet.text.encode("utf-8", "ignore")tweet.text.encode("utf-8", "replace")或{{1}来防止此类错误}}。 tweet.text.encode("utf-8", "backslashreplace")删除任何无法编码的内容; ignorereplace替换侵权字符;并且\ufff为侵权字符backslashreplace添加反斜杠将成为\x00

有关详情:https://docs.python.org/3/howto/unicode.html#converting-to-bytes