我正在尝试从 sunlight foundation 打开此 API 的 URL,并以 JSON 格式从页面返回数据。这是我生成的代码,减去我的 API 密钥周围的括号。
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 仍然没有进展。
原文由 Bradley D. Freeman-Bain 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要使用
from urllib.request import urlopen
,我还建议您在打开连接时使用with
语句。