Bra*_*ain 18 python url urllib python-3.x
我正在尝试从阳光基础打开此API的URL,并从json中的页面返回数据.这是我生成的代码,减去myapikey周围的括号.
import urllib.request.urlopen
import json
urllib.request.urlopen("https://sunlightlabs.github.io/congress/legislators?api_key='(myapikey)")
Run Code Online (Sandbox Code Playgroud)
我得到这个错误
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named request.urlopen
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?香港专业教育学院研究了https://docs.python.org/3/library/urllib.request.html仍然没有进展
sty*_*ane 27
您需要使用from urllib.request import urlopen
,我建议您with
在打开连接时使用该语句.
from urllib.request import urlopen
with urlopen("https://sunlightlabs.github.io/congress/legislators?api_key='(myapikey)") as conn:
# dosomething
Run Code Online (Sandbox Code Playgroud)
在Python 3中,您可以这样实现:
import urllib.request
u = urllib.request.urlopen("xxxx")#The url you want to open
Run Code Online (Sandbox Code Playgroud)
注意:
有些IDE可以import urllib
(Spyder)直接使用,而有些则需要import urllib.request
(PyCharm).
那是因为你有时需要明确地导入你想要的部分,所以当你只需要一小部分时,模块不需要加载所有东西.
希望这会有所帮助.
归档时间: |
|
查看次数: |
59945 次 |
最近记录: |