creepycute
V2EX  ›  问与答

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

  •  
  •   creepycute · Apr 6, 2020 · 3048 views
    This topic created in 2402 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
    also24
    Apr 6, 2020
    1
    你这是有时候返回 JSON Array 有时候返回 JSON Object 的意思?

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

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