使用Tweepy以英语以外的其他语言更新Twitter时出现错误。

问题描述:

我遇到问题,使用字符与ASCII不同的Tweepy。我尝试了许多不同的方法,但他们每个人在终端中都有不同的错误。使用Tweepy以英语以外的其他语言更新Twitter时出现错误。

所以,首先我做的一切,Twitter的询问舞蹈和进口我需要什么:

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) 

然后,我去一个网站上找到的句子来鸣叫。

page = requests.get("www.webpage.com") 
poemT = html.fromstring(page.text) 
phrases = poemT.xpath('//div[@class="mainData"]/text()') 

当尝试转换句话我无法找到适合的功能api.update_satus()的格式,并且我会为每个不同的尝试以下错误消息。

尝试1:

tuite = phrases.pop(0) 
api.update_status(tuite) 

错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128) 

有了这个错误,我发现thisthisthis和其他一些建议解决方案波纹管。但是,我在试图打印句子时没有问题。我想这就是为什么它没有解决我的问题。

尝试2

tuite = phrases.pop(0).encode('utf-8') 
api.update_status(tuite) 

错误:

tweepy.error.TweepError: Failed to send request: Headers indicate a formencoded body but body was not decodable. 

有了这个错误我无法找到任何有关。但是我试图用this的问题来说明ASCII中的句子。但我仍然有错误。

尝试3

title = phrases.pop(0) 
tuite = unicodedata.normalize('NFKD', title).encode('ascii','ignore') 
api.update_status(tuite) 

错误:

tweepy.error.TweepError: [{u'message': u'media_ids parameter is invalid.', u'code': 44}] 

我还发现this的问题,但它涉及与媒体的更新。

最后

我发现this discution上git的提示我的问题是一个bug(?),以固定在Tweepy。

有什么想法?如果我能以api.update_status()接受的格式转换文本,我会很高兴。

我刚刚发现这个链接的答案:

Traceback when updating status on twitter via Tweepy

原来我应该调用更新功能类似如下:

api.update_status(status=tuite)