【问题标题】:Error using Tweepy to update Twitter in another language other than English.使用 Tweepy 以英语以外的其他语言更新 Twitter 时出错。
【发布时间】:2016-02-02 03:16:16
【问题描述】:

我在使用带有不同于 ASCII 字符的 Tweepy 时遇到问题。我尝试了多种不同的方法,但每种方法在终端中都有不同的错误。

所以,首先我按照 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.

由于此错误,我找不到任何相关内容。但我试图将句子转换为 ASCII 格式,就像this 问题所说的那样。但我还是报错了。

试试 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的问题,但它与使用媒体更新有关。

终于

我在 git 上发现了 thisdisution,这表明我的问题是要在 Tweepy 中修复的错误(?)。

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

【问题讨论】:

    标签: python python-2.7 twitter


    【解决方案1】:

    我刚刚在这个链接中找到了答案:

    Traceback when updating status on twitter via Tweepy

    原来我应该这样调用更新函数:

     api.update_status(status=tuite)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      相关资源
      最近更新 更多