我正在尝试制作一个python脚本,可以在outlook中读取JSON数据电子邮件。但是问题是如何从HTML数据中提取JSON数据。这是我要提取的原始JSON数据。在
{
"vpn_detail":
{
"username":"harnishs",
"tokens":
[
"85188605",
"00422786",
],
"cluster_name":"*******.com"
}
}
因此,我在outlook中使用imaplib读取了我的JSON数据,但是它在中读取了JSON数据HTML.So这个JSON数据被转换成HTML,它的read outlook电子邮件是这样的(以HTML格式)
^{pr2}$所以现在从这个HTML数据中,我想要相同的JSON文件, 我把我的代码写成这样
from bs4 import BeautifulSoup
MyStr =""" HTML data """
soup = BeautifulSoup(MyStr, "html.parser")
print(soup.text.strip().replace(" ", ""))
所以这个代码给我这个结果
b'
<!--P{margin-top:0;margi=
n-bottom:0;}-->
{
"vpn_detail":
{
"username":"harnishs&q;=
uot;,
"tokens": =
;
[
=
;"85188605",
=
;"00422786",
=
;"94548619",
=
;
],
"cluster_name":"***********.com"
}
}
'
但我希望这个数据和输入的JSON数据一样,仍然没有对其进行精确的挖掘。建议我任何更改,这样我就可以通过电子邮件获得相同的JSON数据。在
使用
html2text
库可以大大简化您的任务,它几乎可以完成所有的工作,您只需删除不必要的标点符号,并用实数"
替换乱引号:注意事项:
MyStr.decode("utf8")
是必需的html2text.html2text(MyStr.decode("utf8"))
将从字符串中清除HTML,您将立即获得JSONre.sub(r'(&q;=\s*uot;)|=\s*;\s*', lambda x: '"' if x.group(1) else '', MyStrTxt)
删除所有出现在{&q;=
+零个或多个空白+uot;
替换为实数{相关问题 更多 >
编程相关推荐