美丽的汤解析HTML包含JSON
输出了此物体:BS输出此(摘要)
>>> soup.body
<body><p>{
"@context": [
"https://geojson.org/geojson-ld/geojson-context.jsonld",
{
"@version": "1.1",
"wx": "https://api.weather.gov/ontology#",
"@vocab": "https://api.weather.gov/ontology#"
}
],
"type": "FeatureCollection",
"features": [
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.957a95b11de1ec54b622b137ccf43a662d44061f.001.1",
"type": "Feature",
"geometry": null,
"properties": ....(snip)
使用Python3并尝试解析似乎包含JSON对象的NWS天气警报,并从我的了解“ @context”标签中 是JSON数据;那是对的吗?
我如何获得广场和卷曲括号内的元素?
BS显然有JSON解析器,但我没有找到有关这种情况的菜鸟的好教程。
指针最受欢迎。
Using Python3 and trying to parse NWS weather alerts which appear to contain JSON objects using Beautiful Soup and got this far: BS outputs this (snippet from top of output)
>>> soup.body
<body><p>{
"@context": [
"https://geojson.org/geojson-ld/geojson-context.jsonld",
{
"@version": "1.1",
"wx": "https://api.weather.gov/ontology#",
"@vocab": "https://api.weather.gov/ontology#"
}
],
"type": "FeatureCollection",
"features": [
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.957a95b11de1ec54b622b137ccf43a662d44061f.001.1",
"type": "Feature",
"geometry": null,
"properties": ....(snip)
From what I understand the "@context" tag indicates that the subsequent lines within braces are JSON data; is that correct?
How do I get at the elements inside the square and curly braces?
BS apparently has a JSON parser but I haven't found any good tutorials about how-to for someone who's a noob to this situation.
Pointers would be most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该通过一些其他细节来改善问题,如评论中所述,该问题看起来不像,该回答是简单的HTML,而是JSON。
html
汤
是从'lxml'解析器包装的包装的,您不需要
beautifulsoup
no,而不是JSON解析器。而不是在响应 - &gt上使用
.json()
; <docs
Question should be improved by some additional details and as mentioned in the comments it do not look like, that response is plain HTML but rather JSON.
HTML in your
soup
is wrapping from 'lxml' parserYou do not need
beautifulsoup
for that task and no it is not a JSON parser.Instead use
.json()
on your response -> docsExample