代码之家  ›  专栏  ›  技术社区  ›  Mehdi

使用python请求从deta.space获取数据

  •  0
  • Mehdi  · 技术社区  · 2 年前

    我在这里有一个deta.space网站: https://bonbastapi-1-z9030825.deta.app 这是python代码,我正试图从这个网站上获得一些细节:

    def get_last_rate_task():
        url = 'https://bonbastapi-1-z9030825.deta.app/latest'
        try:
            headers = {
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
                'Accept-Encoding': 'gzip, deflate, br, zstd',
                'Accept-Language': 'en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7',
                'Cache-Control': 'max-age=0',
                'Connection': 'keep-alive',
                'Host': 'bonbastapi-1-z9030825.deta.app',
                'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
                'Sec-Ch-Ua-Mobile': '?0',
                'Sec-Ch-Ua-Platform': '"Linux"',
                'Sec-Fetch-Dest': 'empty',
                'Sec-Fetch-Mode': 'navigate',
                'Sec-Fetch-Site': 'same-origin',
                'Upgrade-Insecure-Requests': '1',
                'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
            }
    
            response = requests.get(url=url, headers=headers)
            response.raise_for_status()
            try:
                data = response.json()
                usd_data = data.get('usd', {})
                usd_sell_price = usd_data.get('sell')
                if usd_sell_price is not None:
                    ExchangeRate.objects.create(price=usd_sell_price, source='bonbast')
                else:
                    logger.error(f"USD sell price not found in the response: {data}")
            except ValueError as e:
                logger.error(f"JSON decoding error: {e}, response content: {response.content}")
        except requests.exceptions.HTTPError as http_err:
            logger.error(f"HTTP error occurred: {http_err}, response content: {response.content}")
        except requests.exceptions.RequestException as req_err:
            logger.error(f"Request error occurred: {req_err}")
    

    但我突然犯了这样的错误:

    HTTP error occurred: 403 Client Error: Forbidden for url: https://deta.space/api/v0/auth/pass?redirect_uri=https%3A%2F%2Fbonbastapi-1-z9030825.deta.app%2Flatest, response content: b'<html>\r\n<head><title>403 Forbidden</title></head>\r\n<body>\r\n<center><h1>403 Forbidden</h1></center>\r\n<hr><center>cloudflare</center>\r\n</body>\r\n</html>\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n'
    

    我尝试将其添加到标题中:

                'Cookie': 'deta_app_token=TOKEN FROM deta.space>settings>Authentication>Access Tokens',
    

    但我犯了一个错误:

    HTTP error occurred: 401 Client Error: Unauthorized for url: https://bonbastapi-1-z9030825.deta.app/latest, response content: b'Unauthorized\n'
    

    以上已完全解释

    0 回复  |  直到 2 年前