发送请求失败:报头显示一个格式编码的正文,但正文不可解码



我有一个带有西班牙语字符的列表(在python中),例如:ó, í, ñ,我正在使用Tweepy库,并希望使api.status_update(列表[元素])我得到此错误…

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

列表元素示例:

20个家庭有不同的文化背景,80个家庭有不同的文化背景,

片段代码:

for ultag in sopa.find_all('ul', {'class': 'content'}):
    for litag in ultag.find_all('li'):
        url = litag.find('a')
        textUrl = litag.find('span', {'id': 'noticias_item_extract'})
        urlCorta = shortenerUrl(url.get('href'))
        i = random.randint(75, 81)
        listaCompleta.append(u''.join(textUrl.p.text[:i]+ '...' + ' ' + urlCorta+ ' ' + '#Hash'))

其他片段:

for i in listaCompleta:
    api.update_status(i.encode('utf-8'))
    time.sleep(random.randint(30, 45))

总之:你没有正确编码你的字符。您需要遵循第二个链接中提供的编码,其中它们必须是ASCII字符。

可以使用以下代码片段跟踪错误代码,这些代码片段来自tweepy的init.py

看一下#275行,它在内部被tweepy抬高了,因为:

HTTP请求实体头包含"Content-Type"头字段设置为"application/x-www-form-urlencoded"。

您可以通过HTML文档中的表单获取更多相关信息。

最新更新