如何使用python调用API

问题描述 投票:0回答:1

我使用这段代码已经有一段时间了,效果很好:

# This is needed to send POST and GET requests
import requests
import csv
import numpy
import random

 
# Godaddy developer key and secret
api_key = "##########################"
secret_key = "######################"
 
# API key and secret are sent in the header
headers = {"Authorization" : "sso-key {}:{}".format(api_key, secret_key)}
 
# Domain availability and appraisal end points
appraisal = "https://api.godaddy.com/v1/appraisal/{}"
 

 
# This list holds similar domains sold
# This is retrieved from Godaddy appraisal API
similar_domains = []
domain = []
# This holds available domains found that match
# the search criteria
found_domains = {}
# Open prefix, keyword, suffix and extension from files
with open("keyword.txt") as f:
   keywords = f.read().splitlines()
# csv file
with open("results.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["domain", "similar domain", "price", "year"])

# Filter similar sold domains by sale price and year

    for domain in keywords:
   # Call appraisl API
       appraisal_res = requests.get(appraisal.format(domain), headers=headers).json()
   # Do not abuse the API
       #time.sleep(2)
       delays = [3, 5, 7, 4, 4, 11]
       time.sleep(numpy.random.choice(delays))
       comparable_sales = appraisal_res["comparable_sales"][0]  
       writer.writerow([domain, comparable_sales["domain"], comparable_sales["price"],    comparable_sales["year"]])
       print(f"{domain}")
    

但问题是,当我一个月后回来时,它不起作用。它向我显示这个错误:

Traceback (most recent call last):
  File "C:\Users\LENOVO\Documents\Custom\domaines\lab\already sold finish\ok.py", line 59, in <module>
    appraisal_res = requests.get(appraisal.format(domain), headers=headers).json()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[Finished in 948ms]

如果这条线不再起作用,还可以使用哪些其他方式来调用 api?

我更改了 API 但同样的问题

python json
1个回答
0
投票

您的 API 密钥是否可能已过期?有时它们只能持续一段时间,需要重新生成。

© www.soinside.com 2019 - 2024. All rights reserved.