【发布时间】:2016-04-08 05:30:46
【问题描述】:
我正在 Flask 中创建一个 RSS 阅读器应用程序。在我的程序中,我通过feedparser 包获取提要数据,并且我想以 JSON 格式返回数据。
我的代码在英文网站上运行良好,但是当我想获取任何英文提要时,我收到以下错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
@app.route('/fetch',methods=['POST'])
def fetch():
url = request.form['url']
feed = feedparser.parse(url)
output = {'response':'','result':''}
body = ''
for post in feed.entries:
body += '<div class="post">'
body += '<h3 class="post-title">{}</h3>'.format(post.title)
body += '<div class="post-body">{}</div>'.format(post.summary_detail.value)
body += '</div>'
output['response'] = 'ok'
output['result'] = body
return json.dumps(output, ensure_ascii=False).encode('utf8')
我试过这个:
output['result'] = body.encode('utf8')
return json.dumps(output, ensure_ascii=False)
但我也有同样的问题。
【问题讨论】:
-
请提供完整的追溯。您确定不是
body +=行引发了异常吗?