ArthurOgg
ArthurOgg

Reputation: 23

How do I solve this error with twitter search

I'm having an error while I'm doing a new project that I get data from twitter and record this in a txt file. What should I do to not get this error? I have already tried to turn this on a str ...

The code and the error:

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>

Upvotes: 0

Views: 45

Answers (1)

Thaer A
Thaer A

Reputation: 2323

It's most likely a problem with encoding:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 138-139: character maps to <undefined>

Try:

cd = open("Registro.txt", "a", encoding="utf8")

Upvotes: 2

Related Questions