简体   繁体   English

requests.get 并解析对 json 的响应

[英]requests.get and parse response to json

As the title, I want "requests.get and parse response to json".作为标题,我想要“requests.get 并解析对 json 的响应”。 Here are the ways I have tried to do this以下是我尝试执行此操作的方法

  1. Using request().json()使用 request().json()
requests.get(url = "https://ac.dict.naver.com/linedictweb/ac?q=%E6%B5%B7&st=010&r_lt=000&q_enc=UTF-8&r_format=json&r_enc=UTF-8&_t=1599558293560&_callback=jQuery111104358648061767311_1599558261058&_=1599558261060",verify = True).json()
 =>Result: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

2.Another Way 2.另一种方式

r = requests.get(url = "https://ac.dict.naver.com/linedictweb/ac?q=%E6%B5%B7&st=010&r_lt=000&q_enc=UTF-8&r_format=json&r_enc=UTF-8&_t=1599558293560&_callback=jQuery111104358648061767311_1599558261058&_=1599558261060",verify = True)
json.loads(r.content.decode('utf-8'))
=> RESULT: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Data Json here :数据 Json在这里

Help me to solve this problem.帮我解决这个问题。 Thanks谢谢

The jquery callback is wrapping the output. jquery 回调正在包装输出。 Get rid of the callback parameter in the url and you will get valid json:去掉 url 中的回调参数,您将获得有效的 json:

r = requests.get(url = "https://ac.dict.naver.com/linedictweb/ac?q=%E6%B5%B7&st=010&r_lt=000&q_enc=UTF-8&r_format=json&r_enc=UTF-8&_t=1599558293560",verify = True)


r.json()
{
  "query": [
    "\u6d77"
  ],
  "items": [
    [],
    [
      [
        [
          "zh"
        ],
        [
          "597527adbb7248c3ae80d60b9acd7160"
        ],
        [
          "\u6d77"
        ],
        [
          " ocean; extra large"
        ], 
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM