简体   繁体   English

为什么我不能将请求解码为 JSON?

[英]Why can't I decode the request to JSON?

#!/usr/bin/python3
import re, sys, requests

if len(sys.argv) != 2:
    sys.stderr.write("<Usage> ./script.py message.txt>\n")
    sys.exit(1)

msg = open(sys.argv[1], 'r').read()


group = re.findall(r'(\d+\.\d+)\, (\d+.\d+)', msg)
print(group)
for g in group:
    left_num = float(g[0])
    right_num = float(g[1])
    r = requests.get('https://geocode.xyz/{},{}/json=1'.format(left_num,right_num))
    print(r.json())

I mean, I saw in a different script a really similar thing that worked but for some reason, when I use the .json(), it doesnt let me decode to json我的意思是,我在不同的脚本中看到了一个非常相似的东西,但由于某种原因,当我使用 .json() 时,它不会让我解码为 json

I get this error-我收到此错误-

Traceback (most recent call last):
  File "./script.py", line 17, in <module>
    print(r.json())
  File "/home/idoshany/.local/lib/python3.6/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Replace代替

r = requests.get('https://geocode.xyz/{},{}/json=1'.format(left_num,right_num))

with

r = requests.get('https://geocode.xyz/{},{}?json=1'.format(left_num,right_num))

Have a close look at the ?仔细看看? before json=1json=1之前

/ was the issue here. /是这里的问题。 If you look at the official documentation of the https://geocode.xyz/api , you will come to know about the issue.如果你查看https://geocode.xyz/api的官方文档,你就会知道这个问题。

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

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