Python - 合并 API 分页数据
Python - Merge API Pagination Data
我正在尝试检索每本书的所有页数并计算总数。
下面的解决方案有效,但没有使用 API.
提供的分页功能
import requests
from requests.exceptions import HTTPError
total_number_of_pages = []
def get_all_books():
try:
response = requests.get("https://www.anapioficeandfire.com/api/books?page=1&pageSize=15")
response.raise_for_status()
json_response = response.json()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
return json_response
def sum_of_total_number_of_pages():
total_number_of_pages = sum(pages["numberOfPages"] for pages in get_all_books())
print(f"The total number of pages of all books is {total_number_of_pages}")
sum_of_total_number_of_pages()
我参考了这个 answer 并利用了 API
的分页功能
import requests
from requests.exceptions import HTTPError
baseurl = "https://www.anapioficeandfire.com/api/"
endpoint = "books"
def get_number_of_pages_for_each_book():
number_of_pages_for_each_book = []
try:
response = requests.get(f"{baseurl}{endpoint}")
json_response = response.json()
response.raise_for_status()
for pages in json_response:
number_of_pages_for_each_book.append(pages['numberOfPages'])
while 'next' in response.links:
response = requests.get(response.links['next']['url'])
json_response = response.json()
for pages in json_response:
number_of_pages_for_each_book.append(pages['numberOfPages'])
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
return number_of_pages_for_each_book
def calculate_total_number_of_pages(number_of_pages):
total_amount_of_pages = sum(number_of_pages)
print(f"The total number of pages is {total_amount_of_pages}")
data = get_number_of_pages_for_each_book()
calculate_total_number_of_pages(data)
如果我要 return 来自 get_number_of_pages_for_each_book()
方法的 json_response
它只是 return 来自 while 循环的数据,而不是初始 GET 请求。这就是为什么我选择创建一个列表并附加来自两个请求的数据。
我想知道是否有更好的代码结构方式。
代码看起来不错。
在Python中流行使用while True
与break
import requests
from requests.exceptions import HTTPError
baseurl = "https://www.anapioficeandfire.com/api/"
endpoint = "books"
def get_number_of_pages_for_each_book():
number_of_pages_for_each_book = []
url = f"{baseurl}{endpoint}"
try:
while True:
response = requests.get(url)
json_response = response.json()
for pages in json_response:
number_of_pages_for_each_book.append(pages['numberOfPages'])
if 'next' not in response.links:
break
url = response.links['next']['url']
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
return number_of_pages_for_each_book
def calculate_total_number_of_pages(number_of_pages):
total_amount_of_pages = sum(number_of_pages)
print("The total number of pages is", total_amount_of_pages)
data = get_number_of_pages_for_each_book()
calculate_total_number_of_pages(data)
我正在尝试检索每本书的所有页数并计算总数。 下面的解决方案有效,但没有使用 API.
提供的分页功能import requests
from requests.exceptions import HTTPError
total_number_of_pages = []
def get_all_books():
try:
response = requests.get("https://www.anapioficeandfire.com/api/books?page=1&pageSize=15")
response.raise_for_status()
json_response = response.json()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
return json_response
def sum_of_total_number_of_pages():
total_number_of_pages = sum(pages["numberOfPages"] for pages in get_all_books())
print(f"The total number of pages of all books is {total_number_of_pages}")
sum_of_total_number_of_pages()
我参考了这个 answer 并利用了 API
的分页功能import requests
from requests.exceptions import HTTPError
baseurl = "https://www.anapioficeandfire.com/api/"
endpoint = "books"
def get_number_of_pages_for_each_book():
number_of_pages_for_each_book = []
try:
response = requests.get(f"{baseurl}{endpoint}")
json_response = response.json()
response.raise_for_status()
for pages in json_response:
number_of_pages_for_each_book.append(pages['numberOfPages'])
while 'next' in response.links:
response = requests.get(response.links['next']['url'])
json_response = response.json()
for pages in json_response:
number_of_pages_for_each_book.append(pages['numberOfPages'])
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
return number_of_pages_for_each_book
def calculate_total_number_of_pages(number_of_pages):
total_amount_of_pages = sum(number_of_pages)
print(f"The total number of pages is {total_amount_of_pages}")
data = get_number_of_pages_for_each_book()
calculate_total_number_of_pages(data)
如果我要 return 来自 get_number_of_pages_for_each_book()
方法的 json_response
它只是 return 来自 while 循环的数据,而不是初始 GET 请求。这就是为什么我选择创建一个列表并附加来自两个请求的数据。
我想知道是否有更好的代码结构方式。
代码看起来不错。
在Python中流行使用while True
与break
import requests
from requests.exceptions import HTTPError
baseurl = "https://www.anapioficeandfire.com/api/"
endpoint = "books"
def get_number_of_pages_for_each_book():
number_of_pages_for_each_book = []
url = f"{baseurl}{endpoint}"
try:
while True:
response = requests.get(url)
json_response = response.json()
for pages in json_response:
number_of_pages_for_each_book.append(pages['numberOfPages'])
if 'next' not in response.links:
break
url = response.links['next']['url']
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
return number_of_pages_for_each_book
def calculate_total_number_of_pages(number_of_pages):
total_amount_of_pages = sum(number_of_pages)
print("The total number of pages is", total_amount_of_pages)
data = get_number_of_pages_for_each_book()
calculate_total_number_of_pages(data)