张图南 发表于 2020-11-2 22:24:11

一个执行API的简单程序没报错也没有返回值

代码如下:
import requests

#excute API and restore result
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:",r.status_code)

#restore API feedback into a variable
response_dict = r.json()

#handle result
print(response_dict.keys())

按F5后弹出了C:\WINDOWS\SYSTEM32\cmd.exe窗口,可是啥都没有。

lirenbing01 发表于 2020-11-3 09:19:26

import requests

# excute API and restore result
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR '
                         '2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; '
                         'InfoPath.3)'}

r = requests.get(url, headers=headers)
print("Status code:", r.status_code)

# restore API feedback into a variable
response_dict = r.json()
print(response_dict)
# handle result
print(response_dict.keys())
r.close()
你加个头 不然会认为你是爬虫拒绝你的

张图南 发表于 2020-11-3 19:37:55

lirenbing01 发表于 2020-11-3 09:19
你加个头 不然会认为你是爬虫拒绝你的

还是有问题啊
Traceback (most recent call last):
File "copy.py", line 1, in <module>
    import requests
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\__init__.py", line 120, in <module>
    from . import utils
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\utils.py", line 27, in <module>
    from ._internal_utils import to_native_string
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\_internal_utils.py", line 11, in <module>
    from .compat import is_py2, builtin_str, str
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\compat.py", line 60, in <module>
    from http import cookiejar as cookielib
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\http\cookiejar.py", line 32, in <module>
    import copy
File "C:\Users\asus\Desktop\python_work\copy.py", line 9, in <module>
    r = requests.get(url, headers=headers)
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
我看得有点懵,第一句也报错了,这不是标准库吗;还有get函数,应该没问题才对啊

阿奇_o 发表于 2020-11-3 20:41:05

张图南 发表于 2020-11-3 19:37
还是有问题啊

我看得有点懵,第一句也报错了,这不是标准库吗;还有get函数,应该没问题才对啊

requests模块,你安装了吗 用pip list看一下

张图南 发表于 2020-11-3 20:45:39

阿奇_o 发表于 2020-11-3 20:41
requests模块,你安装了吗 用pip list看一下

安装过了,2.24.0版本的

阿奇_o 发表于 2020-11-3 21:19:11

张图南 发表于 2020-11-3 20:45
安装过了,2.24.0版本的

难道你当前工作目录下有requests.py ?? 同名了,会优先当前的。。
而你这里自然没有定义get方法,有get方法的是pip安装的requests模块,而不是你自己的requests.py

--参考了stackoverflow的回答,可能是这个原因

另外,我试了一下这个API链接,似乎要开VPN全局才能正常连上。。祝你好运吧

阿奇_o 发表于 2020-11-3 21:22:45

张图南 发表于 2020-11-3 20:45
安装过了,2.24.0版本的

参考sof网的回答,可能是你同名了,即当前工作目录下有一个叫requests.py的文件(模块)
即跟pip安装的requests模块 同名了,它优先使用当前的,所以找不到get方法

张图南 发表于 2020-11-3 22:39:17

阿奇_o 发表于 2020-11-3 21:22
参考sof网的回答,可能是你同名了,即当前工作目录下有一个叫requests.py的文件(模块)
即跟pip安装 ...

我也看到过这样的回答,然而并没有重名的文件

张图南 发表于 2020-11-4 20:09:21

假面的假面 发表于 2020-11-4 00:02
你这段代码保存的文件名可能跟某些类模块同名了。建议更换程序保存的文件名再试。

并没有

张图南 发表于 2020-11-5 21:44:26

假面的假面 发表于 2020-11-4 20:31
你是用dos命令行方式直接运行的还是python自带的idle?经测试你的代码无问题。

我用编译器Geany运行的
页: [1]
查看完整版本: 一个执行API的简单程序没报错也没有返回值