I am having problems using Tweepy with characters different than ASCII. I tried a number of different approaches but each of them I have a different Error in the terminal.
So, first I do all the dance that Twitter asks and import what I need:
from lxml import html
import requests
from random import randint
import tweepy, sys, time
import requests
import unicodedata
consumer_key="x"
consumer_secret="y"
access_token="z"
access_token_secret="w"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
Then, I go to a website to find sentences to Tweet.
page = requests.get("www.webpage.com")
poemT = html.fromstring(page.text)
phrases = poemT.xpath('//div[@class="mainData"]/text()')
When trying to convert a sentence I can't find a format that suits the function api.update_satus() and I have the following error messages for each different try.
Try 1:
tuite = phrases.pop(0)
api.update_status(tuite)
Error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
With this error I found this, this, this and a few others suggesting the solution bellow. However, I have no problems when trying to print the sentences. I imagine that is why it did not solve my problem.
Try 2
tuite = phrases.pop(0).encode('utf-8')
api.update_status(tuite)
Error:
tweepy.error.TweepError: Failed to send request: Headers indicate a formencoded body but body was not decodable.
With this error I could not find anything related. But I tried just to transform the sentence in ASCII as this question says. But I still got an error.
Try 3
title = phrases.pop(0)
tuite = unicodedata.normalize('NFKD', title).encode('ascii','ignore')
api.update_status(tuite)
Error:
tweepy.error.TweepError: [{u'message': u'media_ids parameter is invalid.', u'code': 44}]
I also found this question, but it was related to updating with media.
Finally
I found this discution on git suggesting that my problem is a bug(?) to be fixed in Tweepy.
Any thoughts? I would be happy if I could convert the text in a format that api.update_status() accepts.