【发布时间】:2014-09-16 16:08:09
【问题描述】:
我正在抓取网页中的数据,其格式如下:
<!-- Web header up here -->
[{"foo": "Bar", "foo2": "Bar2"},
{"foo3": ["hello", "world"], "foo4": "Bar4"},
...
]
<!-- Web footer here -->
问题是 JSON 与其他内容一起出现在页面上,并且页面源在引号内有 JSON 列表,在 JSON 中带有其他 html 标记的“pre”标记内,如下所示:
<pre>" [{ "foo": "Bar", <p>"foo2": "Bar2"</p>}, ... ] "</pre>
有没有办法绕过这种糟糕的格式并在给定 JSON 对象字符串列表的情况下获取 JSON 对象列表,最好摆脱过程中的嵌入标签?
编辑:我现在已经按照 Mauricio 的建议安装并开始学习 BeautifulSoup4,但我仍然有些不足。在“汤”上使用 .pre 运算符给了我
<pre> [{ ... (Good formatted JSON but inside tags still) ...}]</pre>
HTML:(<pre> 代码只是在其上方和下方有一些标题。)
<pre>
[{
"title": “blah”,
"refs": [“a”, “a”],
"description": [“a”,
“a”,
"a”],
“a”: [
{“a”: “a"}]
},
{
"title": “a”,
"refs": [“a”, “a”],
"description": [“a”,
“a”,
“a”],
“a”: [
{“a”: “a”}]
}]
</pre>
【问题讨论】:
标签: python html json html-parsing beautifulsoup