【发布时间】:2020-03-08 08:13:53
【问题描述】:
您好,我对 python 编程很陌生。在这里,我正在尝试编写一个 python 脚本,它将使用 GET 请求获取状态代码。我可以为单个 URL 执行此操作,但如何在单个脚本中为多个 URL 执行此操作。 这是我编写的基本代码,它将从 url 获取响应代码。
import requests
import json
import jsonpath
#API URL
url = "https://reqres.in/api/users?page=2"
#Send Get Request
response = requests.get(url)
if response:
print('Response OK')
else:
print('Response Failed')
# Display Response Content
print(response.content)
print(response.headers)
#Parse response to json format
json_response = json.loads(response.text)
print(json_response)
#Fetch value using Json Path
pages = jsonpath.jsonpath(json_response,'total_pages')
print(pages[0])
【问题讨论】: