【问题标题】:Extract broken JSON from response从响应中提取损坏的 JSON
【发布时间】:2021-08-17 11:45:59
【问题描述】:

我在 python3 中使用requests 向雅虎登录网站发送帖子请求,在 Burpsuite 中我得到以下响应:

HTTP/1.0 200 OK
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Age: 0
Pragma: no-cache
Expires: 0
Referrer-Policy: origin-when-cross-origin
Cache-Control: no-cache, no-store, must-revalidate
set-cookie: AS=v=1&s=8ZJenFRo&d=A611b9f16|50....OXCxVzImJQI-~A; path=/; domain=login.yahoo.com; secure; HttpOnly
Content-Type: application/json; charset=utf-8
Content-Length: 149
Vary: Accept-Encoding
Date: Tue, 17 Aug 2021 11:20:50 GMT
Strict-Transport-Security: max-age=15552000
Server: ATS

{"location":"/account/challenge/recaptcha?done=https%3A%2F%2Fwww.yahoo.com%2F&sessionIndex=QQ--&acrumb=8ZJenFRo&display=login&authMechanism=primary"}

目标是在python中得到这一行

{"location":"/account/challenge/recaptcha?done=https%3A%2F%2Fwww.yahoo.com%2F&sessionIndex=QQ--&acrumb=8ZJenFRo&display=login&authMechanism=primary"}

我编写了一个简单的脚本来执行发布请求,但是当调用 response.json() 时它崩溃了

import requests
from user_agent import generate_user_agent

def check_yahoo(email):
  yahoo_url = "https://login.yahoo.com"
  data= {"username":f"{email}"}
  heads = {"User-Agent":f"{generate_user_agent()}"}
  response = requests.post(yahoo_url , params=data , headers=heads)
  print (response.json())
  return 

#driver code 
check_yahoo("some.one@yahoo.com")

如果json数据坏了怎么提取

错误

  File "/home/kali/Desktop/yah00/yah00.py", line 49, in <module>
    check_yahoo(item)
  File "/home/kali/Desktop/yah00/yah00.py", line 37, in check_yahoo
    print (response.json())
  File "/usr/lib/python3/dist-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【问题讨论】:

    标签: json python-3.x python-requests


    【解决方案1】:

    json 看起来不错,因此可能是空行将其丢弃。 response.text的内容是什么?如果其中包括空行,则类似于

    json.loads(response.text.strip())
    

    可能就够了。

    【讨论】:

    • 得到同样的错误,你为什么用response.text
    • response.text 是响应的原始文本内容。 json 方法是一种将文本解析为 json 对象的便捷方法。使用text 的想法是查看是否有额外的空格导致解析失败。如果你直接打印response.text,你会得到什么?
    • 我在浏览器中找到的页面源代码相同
    • 那么看起来你需要一个不同的 URL。我对 BurpSuite 不熟悉,你确定你提出了同样的请求吗?
    • 很确定我做到了,甚至用邮递员得到了同样的结果
    【解决方案2】:

    您可以使用 try/except 来调试您的错误,例如:

        def some_function(data):
           try:
              *enter your success code*
           except:
              *enter in error statement*
    

    https://docs.python.org/3/tutorial/errors.html查看更多信息

    【讨论】:

    • 我知道如何处理错误我对json数据中的信息感兴趣
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2017-11-25
    • 2013-03-19
    • 1970-01-01
    • 2013-12-11
    • 2017-01-21
    相关资源
    最近更新 更多