gpt4 book ai didi

python - 解析Python中分块的内容类型的响应

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:48 25 4
gpt4 key购买 nike

我正在尝试读取并解析内容类型的请求:在 python 中分块。以下是我在浏览器中加载 url 并查看源代码时看到的内容:

<!-- ---------------------------------------------------------------- http://github.com/Atmosphere ------------------------------------------------------------------------ --> 
<!-- Welcome to the Atmosphere Framework. To work with all the browsers when suspending connection, Atmosphere must output some data to makes WebKit based browser working.-->
<!-- --------------------------------------------------------------------------------------------------------------------------------------------------------------------- -->
<!-- EOD -->[{"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam2","value":"2.505730663333334E9"}, {"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam1","value":"1.5584484E9"},{"__publicationName":"dip\/acc\/LHC\/Beam\/Energy","value":"495"},

我想检索并解析如下所示的 json 条目:

{"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam2","value":"2.505730663333334E9"}

我该怎么做?

谢谢

最佳答案

“chunked”不是有效的内容类型,尽管它是有效的 transfer-encoding 。根据您发布的示例,这看起来并不是您的问题。这看起来像是应用于常规 jsonp 响应的 header 。在许多情况下,浏览器会忽略 sgml 注释,但您必须手动提取它以供自己使用。这是处理这个问题的想法:

>>> import json
>>> corpus = '''<!-- ---------------------------------------------------------------- http://github.com/Atmosphere ------------------------------------------------------------------------ -->
... <!-- Welcome to the Atmosphere Framework. To work with all the browsers when suspending connection, Atmosphere must output some data to makes WebKit based browser working.-->
... <!-- --------------------------------------------------------------------------------------------------------------------------------------------------------------------- -->
... <!-- EOD -->[{"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam2","value":"2.505730663333334E9"}, {"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam1","value":"1.5584484E9"},{"__publicationName":"dip\/acc\/LHC\/Beam\/Energy","value":"495"}]'''
>>> junk, data = corpus.split('<!-- EOD -->', 1)
>>> parsed = json.loads(data)
>>> for item in parsed:
... print item
...
{u'__publicationName': u'dip/acc/LHC/Beam/Intensity/Beam2', u'value': u'2.505730663333334E9'}
{u'__publicationName': u'dip/acc/LHC/Beam/Intensity/Beam1', u'value': u'1.5584484E9'}
{u'__publicationName': u'dip/acc/LHC/Beam/Energy', u'value': u'495'}

关于python - 解析Python中分块的内容类型的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7424423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com