-1

我正在尝试为我的高级编码课程使用 API,无论我做什么,我总是会收到此错误。我怎么能......呃......没有得到错误?提前致谢。

ETA:我相信我正在使用 IDLE 3.4。

from pprint import pprint
import requests
import encodings.idna
r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London')
pprint(r.json())

现在我收到一条错误消息:

Traceback (most recent call last):
  File "/Users/lilyevans/APIproject.py", line 4, in <module>
    r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London')
AttributeError: 'module' object has no attribute 'get'
4

1 回答 1

0

在 shell 中,您一次不能执行多个语句:

>>> x = 5
y = 6
SyntaxError: multiple statements found while compiling a single statement
You need to execute them one by one:

>>> x = 5
>>> y = 6
>>>

当您看到正在声明多个语句时,这意味着您正在看到一个脚本,该脚本将在稍后执行。但是在交互式解释器中,您一次只能执行一个语句。

尝试运行此代码。确保行由 enter 分隔,而不是 shift enter

from pprint import pprint
import requests
import encodings.idna
r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London')
pprint(r.json())


Out[]:

{u'base': u'cmc stations',
 u'clouds': {u'all': 0},
 u'cod': 200,
 u'coord': {u'lat': 51.51, u'lon': -0.13},
 u'dt': 1415047029,
 u'id': 2643743,
 u'main': {u'humidity': 87,
           u'pressure': 989,
           u'temp': 279.78,
           u'temp_max': 281.15,
           u'temp_min': 278.15},
 u'name': u'London',
 u'sys': {u'country': u'GB',
          u'id': 5091,
          u'message': 0.0299,
          u'sunrise': 1414997905,
          u'sunset': 1415032183,
          u'type': 1},
 u'weather': [{u'description': u'Sky is Clear',
               u'icon': u'01n',
               u'id': 800,
               u'main': u'Clear'}],
 u'wind': {u'deg': 230, u'speed': 2.1, u'var_beg': 190, u'var_end': 260}}
于 2014-11-03T21:17:20.763 回答