【问题标题】:ValueError: No JSON object could be decoded while converting html to jsonValueError:将 html 转换为 json 时无法解码 JSON 对象
【发布时间】:2018-10-04 02:01:12
【问题描述】:

我在执行以下 python 脚本时遇到错误。 问题在于将 html 输出转换为 json 。如果您能帮我解决问题,将非常有帮助。

import urllib2
import json`

url ='http://localhost:40102/cq/etc/replication/agents.author/publish- 
u11cmspu1.html'
username='admin'
password='admin'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)``
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(url)
json_response=json.loads(pagehandle.read())
# output=json.load(pagehandle.read())
print json_response



ERROR :

Traceback (most recent call last):
File "./repQstatus.py", line 18, in <module>
json_response=json.loads(pagehandle.read())
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 338, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

【问题讨论】:

  • 什么是 pagehandle.read() 值?
  • 它基本上是一个 html 输出。
  • 应该是 JSON。请发布print(type(pagehandle.read())pagehandle.read() 的值

标签: python json python-2.7


【解决方案1】:

这里发生的事情是pagehandle.read() 的返回值正在返回一个字符串,python json 库无法将其识别为 JSON。

pagehandle.read() 的响应中肯定还有 JSON,但有些东西使它无法被正确评估。

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 2015-03-29
    • 1970-01-01
    • 2017-11-17
    • 2023-04-03
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多