简体   繁体   English

请求给我错误的URL

[英]Requests giving me error with correct url

I am trying to search an API and when using the requests function I get an error that seems to indicate that there is nothing in JSON on the URL.When putting in my browser it works, and the function works in a similar piece of code. 我正在尝试搜索一个API,当使用请求函数时,我收到一个错误,似乎表明URL上的JSON中没有任何内容。当放入我的浏览器时,它起作用了,并且该函数在类似的代码中起作用。

This is my first ever time trying to code anything, so I getting to here was an effort but now I am just stuck and not sure where my error is. 这是我有史以来第一次尝试编写任何代码,因此我来到这里很费力,但是现在我被困住了,不确定我的错误在哪里。 right now I am printing the URL, and when I put into my browser I find JSON code and the code works in another similar program I was making for testing. 现在,我正在打印URL,当我将其放入浏览器时,我发现JSON代码,并且该代码在我正在测试的另一个类似程序中起作用。

import requests
import time

api = 'https://api.mojang.com/users/profiles/minecraft/'


f = open('Pokemon.txt', 'r')
for line in f:
    url = (api + line)
    print(url)

    json_data = requests.get(url).json()
    Result = (json_data)
    print(result)


Here I get this back: 在这里,我得到了这个:

https://api.mojang.com/users/profiles/minecraft/Bulbasaur

Traceback (most recent call last):
  File "C:\Users\Fierce-PC\Desktop\MC Name project\Pokemon.py", line 12, in <module>
    json_data = requests.get(url).json()
  File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\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)
>>> 


You can clearly see that https://api.mojang.com/users/profiles/minecraft/Bulbasaur works if you click on it and it is in JSON format and I just don't really understand the problem. 您可以清楚地看到,如果您单击https://api.mojang.com/users/profiles/minecraft/Bulbasaur且该格式为JSON格式,则该格式有效,我只是不太了解这个问题。

What confused me more is that this code works 更令我困惑的是,这段代码有效

import urllib.parse
import requests

api = 'https://api.mojang.com/users/profiles/minecraft/'


Name = 'Bulbasaur'

url = (api + Name)
print(url)


json_data = requests.get(url).json()
print(json_data)

And it outputs this like I would want but it will not work with in the loop of looking through every Pokemon 它输出的结果就像我想要的那样,但是在浏览每个神奇宝贝的循环中都无法使用

{'id': '06e299358e2f44f1ad8c5f859d63973b', 'name': 'Bulbasaur'}

Sorry if this is a badly constructed post or that I am missing something really obvious. 抱歉,如果这是一个结构不好的帖子,或者我缺少真正明显的内容。

EDIT: I edited both versions of the code to look like this: 编辑:我编辑了两个版本的代码,如下所示:

    json_data = requests.get(url)
    print(json_data)

And for the print on the second line on the version that works I get this back 对于适用于该版本的第二行上的打印,我将其返回

<Response [200]>

Where as on my main not working program I get this: 在我主要的不工作程序上,我得到以下信息:

<Response [204]>

This as far as I can tell indicates that my code is not working though I am not too sure of the fix still though 据我所知,这表明我的代码无法运行,尽管我不太确定该修复程序

Try this code. 试试这个代码。 It iterates across the lines in your file, constructs the URL and then tries to get the data; 它遍历文件中的各行,构造URL,然后尝试获取数据。 on failure it should print the reason and keep going on the next line. 如果失败,则应打印原因并继续进行下一行。

with open('Pokemon.txt') as fh:
    for line in fh:
        url = (api + line.strip())
        print(url)

        conn = requests.get(url)
        if not conn.ok:
            print("Failure on {}: {}".format(url, conn.reason))
            continue
        result = conn.json()
        print(result)

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

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