-1

我被错误和到期提交严重困扰。

        conn = http.client.HTTPSConnection("api.themoviedb.org/3")
        conn.request('GET', '/person/5064/movie_credits?api_key={api_key}&language=en-US')                                         
        r1 = conn.getresponse()
        print(r1.status, r1.reason)
        data1 = r1.read()
        print(type(data1))
        print(data1)        

我尝试使用我的 api 密钥从浏览器访问 api,它可以工作。在笔记本电脑上运行代码时,出现以下错误:

C:\Users\Dhaval Desai\Desktop\Gatech\DVA\hw1-skeleton>"C:/Users/Dhaval Desai/AppData/Local/Programs/Python/Python37/python.exe" "c:/Users/Dhaval Desai/Desktop/Gatech/DVA/hw1-skeleton/Q1/submission.py"
Traceback (most recent call last):
  File "c:/Users/Dhaval Desai/Desktop/Gatech/DVA/hw1-skeleton/Q1/submission.py", line 311, in <module>
    tmdb_api_utils.get_movie_credits_for_person("5064")
  File "c:/Users/Dhaval Desai/Desktop/Gatech/DVA/hw1-skeleton/Q1/submission.py", line 214, in get_movie_credits_for_person
    conn.request("GET", "/movie/550?api_key=8dcea7325ee7c9571a21d3184cffdf34&language=en-US")
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 966, in send
    self.connect()
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1414, in connect
    super().connect()
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 938, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\Dhaval Desai\AppData\Local\Programs\Python\Python37\lib\socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
4

1 回答 1

1

您尝试访问的主机名是"api.themoviedb.org/3"错误的,它不是主机名。

正如错误回溯所示,这是发生错误的第二行 - 大概http.client.HTTPSConnection()直到那时才真正查找您提供的主机名。

例如,尝试解析您的主机名会给出完全相同的错误(尽管行号不同,但我在 Windows x64 上使用的是 Python 3.8.3):

>>> import socket
>>> socket.getaddrinfo("api.themoviedb.org/3",443)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python38\lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

只需删除/3?

于 2020-09-07T13:25:41.737 回答