Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python从HTML数据中提取JSON数据?_Python_Html_Json_Regex - Fatal编程技术网

如何使用Python从HTML数据中提取JSON数据?

如何使用Python从HTML数据中提取JSON数据?,python,html,json,regex,Python,Html,Json,Regex,我正在尝试制作一个python脚本,可以读取outlook电子邮件中的JSON数据。但问题是如何从HTML数据中提取JSON数据。这是我想要提取的原始JSON数据 { "vpn_detail": { "username":"harnishs", "tokens": [ "85188605", "00422786", ],

我正在尝试制作一个python脚本,可以读取outlook电子邮件中的JSON数据。但问题是如何从HTML数据中提取JSON数据。这是我想要提取的原始JSON数据

{
"vpn_detail":
    {
        "username":"harnishs",  
        "tokens":   
            [
                "85188605",
                "00422786",
            ],
        "cluster_name":"*******.com"
    }

}
所以我在outlook中使用imaplib读取了我的JSON数据,但它是在HTML中读取JSON数据的。所以这个JSON数据被转换成HTML,它的读取outlook电子邮件是这样的(以HTML形式)

所以这个代码给了我这个结果

b'


<!--P{margin-top:0;margi=
n-bottom:0;}-->




{
"vpn_detail":
   {
      "username":"harnishs&q;=
uot;,   
      "tokens":   =
;
         [
            =
;"85188605",
            =
;"00422786",
            =
;"94548619",
            =
;
          ],
      "cluster_name":"***********.com"
   }

}





'
b'
{
“vpn_详细信息”:
   {
“用户名”:“Harnish&q=
uot
“代币”:=
;
         [
            =
;"85188605",
            =
;"00422786",
            =
;"94548619",
            =
;
          ],
“群集名称”:“************.com”
   }
}
'

但我希望这些数据与输入的JSON数据相同,但仍然没有精确的挖掘。请建议我进行任何更改,以便我可以通过电子邮件获得相同的JSON数据。

您可以使用
html2text
库大大简化您的任务,该库几乎可以完成所有工作,您只需要删除不必要的标点符号并替换乱七八糟的引号用实数标记

导入re、json、html2text MyStr=b'\r\n\r\n\r\n\r\n\r\n\r\n\r\n

\r\n\r\n{
\r\n“vpn\u详细信息”:
\r\n{
\r\n“用户名”:“kushpate&q=\r\n=\r\n,
\r\n”令牌“:\r\n>\r\n\r\n”=\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\n\r\n\n\n\r\n\n\r\n\n\r\n\n\r\r\n\r\n\n\r\n\r\n\n\n\r\r\n\n\r\n\n\r\r\r\n\r\r\n\n\n\r\n\r\r\n\r\r\n\n\n\n\r\n\r\n\n\r\n\n\r\r\n\n\r\n\n\n\n\r\n\r\n;\n' MyStrTxt=html2text.html2text(MyStr.decode(“utf8”)) clean_string=re.sub(r'(&q;=\s*uot;)|=\s*;\s*',lambda x:''如果x.group(1)else',MyStrTxt) js=json.load(干净的字符串) 打印(js['vpn_detail']['username']) #=>“kushpate” 注:

  • 您的输入字符串是一个字节字符串,您需要将其转换为Unicode UTF8字符串,因此,
    MyStr.decode(“UTF8”)
    是必需的
  • html2text.html2text(MyStr.decode(“utf8”))
    将清除字符串中的HTML,您将立即获得JSON
  • re.sub(r'(&q;=\s*uot;)|=\s*;\s*,lambda x:“'if x.group(1)else”“,MyStrTxt)
    删除所有出现的
    =之间有空格(如果有),或将替换
    &q=+零个或多个空格+
    uot带有一个真正的
    字符

如果您将JSON数据作为纯文本电子邮件而不是HTML电子邮件发送,您的机会将大大提高。并且您需要指定一种编码来消除所有的
&q;=
标记…尝试
re.search(r'{\s*“\w+”*},soup.text.strip().decode(“utf8”),re.s.group()
@WiktorStribiżew是的,我的输出得到了改进,但仍然没有得到确切的JSON数据。我想从用户名中删除这一行(&q;=uot;),现在我的输出是这样的,```{“vpn_详细信息”:{“username”:“harnish&q;=uot;,“tokens=”;[=;“85188605”,“00422786”,=;“94548619”,“51249494”,“HHEF0EA5”,“2E09A81E”],“集群名称”:“bgl13-=vpn-cluster-2.cisco.com”}```
re.search(r'{\s*“\w+”*”,s.decode(“UTF8”),re.s.group().strip()。替换('&q;=\nuot;“,”)
?换行符总是在
=
之后出现吗?谢谢@WiktorStribizew的回答,但我不想手动删除,因为每次我使用python阅读电子邮件时,这些JSON数据都会不同,但格式相同,所以我的问题是如何从HTML数据中获取相同的JSON数据。您的代码只给出了答案[kushpate]我想要完整的JSON数据。@Harnishhah
print(js)
。请玩转这些代码。你感兴趣的任何变量都很容易
print()
。是的,它正在工作。感谢@Wiktor提供最佳解决方案。
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"
   }

}





'
import re, json, html2text

MyStr = b'<html>\r\n<head>\r\n<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=\r\n1">\r\n<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=\r\nn-bottom:0;} --></style>\r\n</head>\r\n<body dir=3D"ltr">\r\n<div id=3D"divtagdefaultwrapper" dir=3D"ltr" style=3D"font-size: 12pt; colo=\r\nr: rgb(0, 0, 0); font-family: Calibri, Helvetica, sans-serif, &quot;EmojiFo=\r\nnt&quot;, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, NotoCo=\r\nlorEmoji, &quot;Segoe UI Symbol&quot;, &quot;Android Emoji&quot;, EmojiSymb=\r\nols;">\r\n<p style=3D"margin-top:0; margin-bottom:0"></p>\r\n<div>\r\n<div>{<br>\r\n&quot;vpn_detail&quot;:<br>\r\n&nbsp;&nbsp; &nbsp;{<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&quot;username&quot;:&quot;kushpate&q=\r\nuot;,&nbsp;&nbsp; &nbsp;<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&quot;tokens&quot;:&nbsp;&nbsp; &nbsp=\r\n;<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;[<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp=\r\n;&quot;85188605&quot;,<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp=\r\n;&quot;00422786&quot;,<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp=\r\n;&quot;94548619&quot;,<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp=\r\n;&quot;51249494&quot;,<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp=\r\n;&quot;HHEF0EA5&quot;,<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp=\r\n;&quot;2E09A81E&quot;<br>\r\n&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;],<br>\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&quot;cluster_name&quot;:&quot;bgl13-=\r\nvpn-cluster-2.cisco.com&quot;<br>\r\n&nbsp;&nbsp; &nbsp;}<br>\r\n<br>\r\n}</div>\r\n</div>\r\n<br>\r\n<p></p>\r\n</div>\r\n</body>\r\n</html>\r\n'
MyStrTxt = html2text.html2text(MyStr.decode("utf8"))
clean_string = re.sub(r'(&q;=\s*uot;)|=\s*;\s*', lambda x: '"' if x.group(1) else '', MyStrTxt)
js = json.loads(clean_string)
print(js['vpn_detail']['username']) 
# => 'kushpate'