[英]How do I solve this error with twitter search
我在做一個新項目時出錯,我從 twitter 獲取數據並將其記錄在 txt 文件中。 我應該怎么做才能不出現此錯誤? 我已經嘗試將其打開...
代碼和錯誤:
from TwitterSearch import *
cd = open("Registro.txt", "a")
try:
tso = TwitterSearchOrder()
tso.set_language('pt')
tso.set_keywords(['rats'])
ts = TwitterSearch('') # i erased it because its my personal dates and etc.
todo = True
next_max_id = 0
while(todo):
response = ts.search_tweets(tso)
todo = not len(response['content']['statuses']) == 0
for tweet in response['content']['statuses']:
tweet_id = tweet['id']
print(f"Seen tweet with ID {tweet_id}")
print(tweet['text'])
cd.write(tweet['text'])
if (tweet_id < next_max_id) or (next_max_id == 0):
next_max_id = tweet_id
next_max_id -= 1
tso.set_max_id(next_max_id)
except TwitterSearchException as e:
print(e)
~~~~~~~the error~~~~~~~
Traceback (most recent call last):
File "", line 22, in <module>
cd.write(tweet['text'])
File "", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 138-139: character maps to <undefined>
這很可能是編碼問題:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 138-139: character maps to <undefined>
嘗試:
cd = open("Registro.txt", "a", encoding="utf8")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.