使用请求模块时引发InvalidSchema(“找不到{!r}的连接适配器”.format(url)

tyu7yeag  于 2022-10-22  发布在  Python
关注(0)|答案(1)|浏览(227)

每次for循环调用API时,我都会遇到这个错误。奇怪的是,对于第一个调用有问题的查询,我仍然得到json对象结果,

Traceback (most recent call last):

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args,**callback_kwargs)
  File "/Users/tonyhall/Desktop/Coding_Stuff/pilots/script_app/views.py", line 38, in index
    response = requests.get(r)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params,**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url,**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep,**send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 649, in send
    adapter = self.get_adapter(url=request.url)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 742, in get_adapter
    raise InvalidSchema("No connection adapters were found for {!r}".format(url))
requests.exceptions.InvalidSchema: No connection adapters were found for '{\'page\': 1, \'results\': [{\'backdrop_path\': \'/c4CSgKL6QfkJxsWcGYDyTxpbzpW.jpg\', \'first_air_date\': \'2017-03-31\', \'genre_ids\': [18, 9648], \'id\': 66788, \'name\': \'13 Reasons Why\', \'origin_country\': [\'US\'], \'original_language\': \'en\', \'original_name\': \'13 Reasons Why\', \'overview\': "After a teenage girl\'s perplexing suicide, a classmate receives a series of tapes that unravel the mystery of her tragic choice.", \'popularity\': 88.15, \'poster_path\': \'/nel144y4dIOdFFid6twN5mAX9Yd.jpg\', \'vote_average\': 7.7, \'vote_count\': 3489}, {\'backdrop_path\': \'/8InkNNcz5MGZUgeuZA58nQ1glGJ.jpg\', \'first_air_date\': \'2017-03-31\', \'genre_ids\': [10767], \'id\': 80865, \'name\': \'13 Reasons Why: Beyond the Reasons\', \'origin_country\': [\'US\'], \'original_language\': \'en\', \'original_name\': \'13 Reasons Why: Beyond the Reasons\', \'overview\': \'Cast members, writers, producers and mental health professionals discuss some of the difficult issues and themes explored in "13 Reasons Why."\', \'popularity\': 10.454, \'poster_path\': \'/aauFzhjAGSemDpDBSmWDJpkeQiw.jpg\', \'vote_average\': 6.1, \'vote_count\': 21}], \'total_pages\': 1, \'total_results\': 2}'
[22/Oct/2022 04:34:36] "GET / HTTP/1.1" 500 101262
/Users/tonyhall/Desktop/Coding_Stuff/pilots/script_app/views.py

在做了一些研究之后,我开始思考我的url在下面的某个地方添加了一个额外的“”或“”,或者它可能会混淆字符串数据类型,因为它的标题中有整数:“12 Monkeys”,“90210”。
如何检查并修复此问题?
这是我的相关代码:

from email.mime import base
import pprint
from pkgutil import get_data
from pydoc import pager
from unittest import result
from django.http import JsonResponse
from django.shortcuts import render, HttpResponse, redirect
from .models import *
import json
from django.core.paginator import Paginator
from django.db.models import Q
import requests
from pprint import pp, pprint
...

for lists in post:            
        data = requests.get(F'https://api.themoviedb.org/3/search/tv?api_key={api_key}&language=en-US&page=1&include_adult=false&query={lists}')

        r = data.json()
    response = requests.get(r)
    response.raise_for_status()  # raises exception when not a 2xx response
    if response.status_code != 204:
        return response.json()
6rqinv9w

6rqinv9w1#

调用response = requests.get(r)时可能会出现错误。请求需要URL,但得到了奇怪的'{\'page\': 1, \'results\': [{\ ...字符串。您从第一个请求中得到的响应是一个字典,而不是URL。
您可以使用print(r),查看您想要的URL是否在响应中的某个位置。

相关问题