-1

我有一个 html 代码,其中有一个由另一个程序生成的 json 字符串,并且整个 json 字符串在 html 代码中进行了注释。但是有一些重要的信息必须从 json 中解析出来。我可以做些什么来将注释的 json 字符串也转换为 html 格式,以便它成为我可以解析的正确 html 代码。

这是输入样本。由于字符限制,我剥离了一些代码。

<!DOCTYPE html> 
 <!--[if lt IE 7]> <html lang="en" class="ie ie6 lte9 lte8 lte7 os-win"> <![endif]-->
 <!--[if IE 7]> <html lang="en" class="ie ie7 lte9 lte8 lte7 os-win"> <![endif]-->
 <!--[if IE 8]> <html lang="en" class="ie ie8 lte9 lte8 os-win"> <![endif]-->
 <!--[if IE 9]> <html lang="en" class="ie ie9 lte9 os-win"> <![endif]-->
 <!--[if gt IE 9]> <html lang="en" class="os-win"> <![endif]-->
 <!--[if !IE]><!--> <html lang="en" class="os-win"> <!--<![endif]-->

<head>

<meta name="lnkd-track-json-lib" content="http://s.c.lnkd.licdn.com/scds/concat/common/js?h=2jds9coeh4w78ed9wblscv68v-eo3jgzogk6v7maxgg86f4u27d&amp;fc=2">
  <meta name="lnkd-track-lib" content="http://s.c.lnkd.licdn.com/scds/concat/common/js?h=eo3jgzogk6v7maxgg86f4u27d&amp;fc=2"><meta name="treeID" content="yGlqHfV7FxMQvJqjACsAAA==">
  <meta name="appName" content="profile">
<meta name="lnkd-track-error" content="/lite/ua/error?csrfToken=ajax%3A1584468784299534813&amp;goback=%2Enpv_131506997_*1_*1_NAME*4SEARCH_9ikF_*1_en*4US_*1_*1_*1_123452511375704499972_1_63_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1"><script src="http://static.licdn.com:80/scds/common/u/lib/fizzy/fz-1.3.3-min.js" type="text/javascript"></script><script type="text/javascript">fs.config({"failureRedirect":"http://www.linkedin.com/nhome/","uniEscape":true,"xhrHeaders":{"X-FS-Origin-Request":"/profile/view?id=131506997&authType=NAME_SEARCH&authToken=9ikF&locale=en_US&srchid=123452511375704499972&srchindex=1&srchtotal=63&trk=vsrp_people_res_name&trkInfo=VSRPsearchId%3A123452511375704499972%2CVSRPtargetId%3A131506997%2CVSRPcmpt%3Aprimary","X-FS-Page-Id":"nprofile-view"}});</script>
<!--{"content":{"search_highlight":{},"message_exchanged":{"messagesOnlyToViewee":true,"messagesOnlyToViewer":true},"Certifications":{"certsMpr":{},"empty":{}},"lix_treasury_callout":"B","network_overview":{"lix_deferLoad":"B","lix_showDetail":"control","distance":3,"lix_deferOnload":"B","allow_pivot_search":false,"i18n_S_NETWORK":"xyz's Network","facets":{"skill_explicit":{"data":[{"count":5,"name":"Equity Research","value":"2112"},{"count":5,"name":"Equities","value":"462"},{"count":5,"name":"Portfolio Management","value":"480"},{"count":4,"name":"Financial Markets","value":"1371"},{"count":4,"name":"Derivatives","value":"814"}]}} }}}}

我尝试取出 json 部分并尝试通过

>>> json1 = json.loads(f1)

Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    json1 = json.loads(f1)
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
4

1 回答 1

1

您可以使用 解析来自 html 的注释,然后通过模块lambda text:isinstance(text, Comment)加载 json 字符串。json这是一个例子:

import json
from bs4 import BeautifulSoup, Comment

soup = BeautifulSoup("""
<table>
<tr>
   <td><table><tr><td>1</td></tr><tr><td>2</td></tr></table></td>
</tr>
<!--

{"test": [1,2,3]}

-->
<tr>
   <td><table><tr><td>3</td></tr><tr><td>4</td></tr></table></td>
</tr>
</table>
""")

comments = soup.find(text=lambda text:isinstance(text, Comment))
comments = json.loads(comments)
print comments['test']

印刷:

[1,2,3]

希望有帮助。

于 2013-08-08T08:04:03.613 回答