首页 > 解决方案 > python - 如何使用feedgen在RSS提要中显示法语字符编码

问题描述

我正在使用Feedgen生成 RSS 提要,但如何设置编码以显示法语字符。

应用程序.py

# Flask package
# pip install flask
from flask import make_response
from flask import Flask, Response

# Other Python external packages
# pip install feedgen
from feedgen.feed import FeedGenerator

app = Flask(__name__, static_url_path='')

def get_feed_object():
    fg = FeedGenerator()
    fg.title("STO Bug")
    fg.description(
        "STO question"
    )
    fg.link(href="https://sto.com")
    return fg

@app.route("/", methods=("GET",))
def rss():

    fg = get_feed_object()

    downloads = [
        {
            'Survey': "Enquête sociale générale, cycle 29",
        },
        {
            'Survey': 'Enquête sociale générale, cycle 30',
        }
    ]

    for survey in downloads[:20]:
        fe = fg.add_entry()
        fe.title(
            survey["Survey"]
        )

    response = make_response(fg.rss_str(encoding='UTF-8'))
    response.headers.set("Content-Type", "application/rss+xml")

    return response


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

在此处输入图像描述

标签: pythonflaskrss

解决方案


我需要在 response.header 中设置编码

response.headers.set("Content-Type", "application/rss+xml; charset=utf-8")

推荐阅读