import urllib.request.urlopen
import json
urllib.request.urlopen("https://sunlightlabs.github.io/congress/legislators?api_key='(myapikey)")
我发现了这个错误
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named request.urlopen
我做错什么了?我研究过https://docs.python.org/3/library/urllib.request.html,但仍然没有进展
在Python 3中,您可以这样实现:
注意: 有些IDE可以直接
import urllib
(Spyder),而有些则需要import urllib.request
(PyCharm)。这是因为有时需要显式地导入所需的片段,所以当只需要其中一小部分时,模块不需要加载所有内容。
希望这会有帮助。
您需要使用
from urllib.request import urlopen
,我还建议您在打开连接时使用with
语句。相关问题 更多 >
编程相关推荐