首页 > 解决方案 > 用python和json编码

问题描述

我不明白如何使用 python 进行编码。

import json
from bs4 import BeautifulSoup


soup = BeautifulSoup('A lot of HTML here', 'html.parser')
rec_titre = soup.find_all("div", {"class": "c-toparticle__title"})
trait_titre = rec_titre[0].text.replace("\n", '').strip()

dataEcho.append({'Titre': trait_titre, 'content': trait_content, 'uri': "https://www.lecho.be" + rec_uri})


with open('data.json', 'w') as outfile:
    json.dump(dataEcho, outfile, sort_keys=True, indent=4, separators=(',', ': '))

结果是我的 Json 文件:

[
    {
        "Titre": "Amay | Vers un troisi\u00c3\u00a8me mandat pour Jean-Michel Javaux",
        "content": "L\u00e2\u0080\u0099ex copr\u00c3\u00a9sident d\u00e2\u0080\u0099Ecolo a demand\u00c3\u00a9 (et obtenu) une d\u00c3\u00a9rogation de son parti Ecolo pour pouvoir briguer un nouveau mandat ma\u00c3\u00aforal.",
        "uri": "https://www.lecho.be/dossier/zoom-communal/amay-vers-un-troisieme-mandat-pour-jean-michel-javaux/10032845.html"
    }
]

编辑 :

我一直在寻找今天早上不可能找到。我在python3

代码在这里|| 只是想在我的 json 中有 utf-8 ......

杰森:

[
  {
    "Titre": "Amay | Vers un troisième mandat pour Jean-Michel Javaux",
    "content": "Lâex coprésident dâEcolo a demandé (et obtenu) une dérogation de son parti pour pouvoir briguer un nouveau mandat maïoral.",
    "uri": "https://www.lecho.be/dossier/zoom-communal/amay-vers-un-troisieme-mandat-pour-jean-michel-javaux/10032845.html"
  }
]

标签: pythonjsonencryptionbeautifulsoupencode

解决方案


推荐阅读