如何将Python中的请求库用于多个参数?
我正在用Python编写一个程序,该程序通过使用MOT交易API获取车辆注册并返回其MOT历史记录。到目前为止,我已经有了一辆车的程序。我希望程序通过导入包含车辆登记列表的excel文件来查找多辆车辆。这是我的密码:如何将Python中的请求库用于多个参数?,python,python-requests,Python,Python Requests,我正在用Python编写一个程序,该程序通过使用MOT交易API获取车辆注册并返回其MOT历史记录。到目前为止,我已经有了一辆车的程序。我希望程序通过导入包含车辆登记列表的excel文件来查找多辆车辆。这是我的密码: import requests from datetime import datetime import smtplib import time import pandas as pd df = pd.read_excel(r'vehicles.xlsx') # Reading
import requests
from datetime import datetime
import smtplib
import time
import pandas as pd
df = pd.read_excel(r'vehicles.xlsx') # Reading the excel file with the vehicle database
reg = df['registration']
headers = {
'Accept': 'application/json',
'x-api-key': apiKey,
}
params = (
('registration', reg),
)
response = requests.get('https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests',
headers=headers, params=params)
def check_days_left():
r = response.json() # This returns a list of dicts of the full MOT History data
carDetails = r[0] # Access the first dictionary of the list which is MOT tests
motTests = carDetails['motTests'] # This returns a list of dicts of the MOT tests
latestTest = motTests[0] # Access the first dictionary to get the details of the latest test
expDate = latestTest['expiryDate'] # This returns the expiry date key of the latest test dictionary
uk_date = datetime.strptime(expDate, '%Y.%m.%d') # Convert expDate to datetime format
difference = uk_date - datetime.now() # Gets the timedelta between now and expiry date
days_left = difference.days # Extract the number of days from the above as an int
print(days_left)
check_days_left()
此时,它将返回excel文件中最后一辆车的MOT详细信息,而忽略其余信息。非常感谢您的帮助。谢谢 您可以循环查看所有注册号并请求api以获得每个注册号的结果 工作解决方案可能类似于以下代码-
import requests
from datetime import datetime
import smtplib
import time
import pandas as pd
df = pd.read_excel(r'vehicles.xlsx') # Reading the excel file with the vehicle database
registrations = df['registration'].tolist()
r = []
# loop through all the registration number in registrations array.
for reg in registrations:
headers = {
'Accept': 'application/json',
'x-api-key': apiKey,
}
params = {
'registration': reg,
}
# Add the response from the api in the array `r`
r.append(requests.get('https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests', headers=headers, params=params).json())
def check_days_left():
r = response.json() # This returns a list of dicts of the full MOT History data
carDetails = r[0] # Access the first dictionary of the list which is MOT tests
motTests = carDetails['motTests'] # This returns a list of dicts of the MOT tests
latestTest = motTests[0] # Access the first dictionary to get the details of the latest test
expDate = latestTest['expiryDate'] # This returns the expiry date key of the latest test dictionary
uk_date = datetime.strptime(expDate, '%Y.%m.%d') # Convert expDate to datetime format
difference = uk_date - datetime.now() # Gets the timedelta between now and expiry date
days_left = difference.days # Extract the number of days from the above as an int
print(days_left)
check_days_left()
使用for循环遍历所有注册号。