V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
creepycute
V2EX  ›  问与答

请问如何提取 Python Requests 返回内容中的字段

  •  
  •   creepycute · Apr 6, 2020 · 2595 views
    This topic created in 2214 days ago, the information mentioned may be changed or developed.
    小白一枚 最近用 git 上接口弄了个学习通自动签到的 但是接口返回的数据有时候不一样
    [{"name":"test","date":"04-06 10:10","status":"success"}]
    {'name': 'test', 'date': '2020-04-06 20:59:51', 'status': '学生端-签到成功'}

    试过 json.loads 和 r.json 都没办法将 r[‘status’]的内容提取出来 总是报错 求教
    9 replies    2020-04-07 08:00:33 +08:00
    also24
        1
    also24  
       Apr 6, 2020
    你这是有时候返回 JSON Array 有时候返回 JSON Object 的意思?

    Requests 的 response 对象自带 json() 方法可以做反序列化。

    判断一下反序列化后的对象类型吧。
    creepycute
        2
    creepycute  
    OP
       Apr 6, 2020
    @also24 好像是这样的 两种返回类型好像是随机的…能不能直接提取出 status 的状态呢?
    Trim21
        3
    Trim21  
       Apr 6, 2020
    data = r.json()
    if isinstance(data, list): pass
    also24
        4
    also24  
       Apr 6, 2020
    @creepycute #2
    判断一下反序列化后的对象类型然后根据不同的类型去取啊。
    omph
        5
    omph  
       Apr 6, 2020
    用正则,很暴力
    Enying
        6
    Enying  
       Apr 6, 2020 via Android
    你那返回的是列表
    noobsheldon
        7
    noobsheldon  
       Apr 7, 2020
    if 'success' in str(response) or "成功" in str(response): pass /doge
    jugelizi
        8
    jugelizi  
       Apr 7, 2020 via iPhone
    别想多 字符串包含来处理
    renmu
        9
    renmu  
       Apr 7, 2020 via Android
    三楼正解
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2691 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 15:47 · PVG 23:47 · LAX 08:47 · JFK 11:47
    ♥ Do have faith in what you're doing.