Python - http.client:对 TMDB API 的 GET 请求失败错误

Python - http.client : GET request to TMDB API failed Error

我被错误和提交到期所困。

        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

您尝试访问的主机名是 "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?