我遇到問題,使用字符與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)
有了這個錯誤,我發現this,this,this和其他一些建議解決方案波紋管。但是,我在試圖打印句子時沒有問題。我想這就是爲什麼它沒有解決我的問題。
嘗試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()接受的格式轉換文本,我會很高興。