search

Home  >  Q&A  >  body text

python json的相关操作

第一步
(已经得到data)

{
"data": [
      "id": "123456",
      "driver": "2016-03-28",
      "phone": "13800138000",
      "type": "A",
    },
    {
      "id": "456789",
      "driver": "2016-03-28",
      "phone": "13800138111",
      "type": "B",
    },
    {
      "id": "13579",
      "driver": "2016-03-28",
      "phone": "138001382222",
      "type": "B",
    }
  ]
}

第二步
只需要输出data数据中的"phone"
13800138000
13800138111
138001382222

第三步
网址如下

http://httpbin.org/response-headers?Content-Type=text%2Fplain%3B+charset%3DUTF-8&phone=phone

phone = phone
遍历第二步的phone 替换这个phone
进行批量下载json

应该是需要
import json
import requests
import os

巴扎黑巴扎黑2889 days ago326

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 17:53:38

    import json
    
    import requests
    
    data = '''
    {
    "data": [
    {
          "id": "123456",
          "driver": "2016-03-28",
          "phone": "13800138000",
          "type": "A"
        },
        {
          "id": "456789",
          "driver": "2016-03-28",
          "phone": "13800138111",
          "type": "B"
        },
        {
          "id": "13579",
          "driver": "2016-03-28",
          "phone": "138001382222",
          "type": "B"
        }
      ]
    }'''
    url_template = 'http://httpbin.org/response-headers?Content-Type=text%2Fplain%3B+charset%3DUTF-8&phone={phone}'
    json_loads = json.loads(data)
    for item in json_loads["data"]:
        phone = item["phone"]
        url = url_template.format(phone=phone)
        print requests.get(url).text
    

    Please note that your json data does not comply with standard json specifications, you should use my data.

    reply
    0
  • Cancelreply