用yweather做的简单测试失败了

2024-09-28 22:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

import yweather

client = yweather.Client()
id = client.fetch_woeid("Beijing, China")
beijing_weather = client.fetch_weather(id)

又好又简单。。。就在yweather的文档中,我得到了一个错误:

> Traceback (most recent call last):
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):   File
> "C:/Users/DSHenderson/PycharmProjects/WeatherAPI/Main.py", line 4, in
> <module>
>     beijing_weather = client.fetch_weather(id)   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\site-packages\yweather.py",
> line 180, in fetch_weather
>     rss = self._fetch_xml(url)   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\site-packages\yweather.py",
> line 344, in _fetch_xml
>     with contextlib.closing(urlopen(url)) as f:   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\urllib\request.py",
> line 223, in urlopen
>     return opener.open(url, data, timeout)   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\urllib\request.py",
> line 526, in open
>     response = self._open(req, data)   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\urllib\request.py",
> line 544, in _open
>     '_open', req)   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\urllib\request.py",
> line 504, in _call_chain
>     result = func(*args)   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\urllib\request.py",
> line 1346, in http_open
>     return self.do_open(http.client.HTTPConnection, req)   File "C:\Users\DSHenderson\AppData\Local\Programs\Python\Python36\lib\urllib\request.py",
> line 1320, in do_open
>     raise URLError(err) urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>

这有点令人沮丧。有什么想法吗?你知道吗

编辑:为便于阅读,将回溯转换为“代码”。你知道吗

Edit2:现在使用forecastiopy,似乎能以同样的方式完成工作。你知道吗


Tags: inpyselfclientrequestliblocalline