import requests
def paginate_response_headers(url, max_pages=5):
"""
Paginates response headers for debugging, handling edge cases.
Args:
url (str): The URL to fetch headers from.
max_pages (int): Maximum number of pages to retrieve.
Returns:
list: A list of dictionaries, where each dictionary represents a page
of response headers. Returns an empty list if there's an error.
"""
all_headers = []
for page in range(1, max_pages + 1):
try:
response = requests.head(url, allow_redirects=True) #Use HEAD request for efficiency
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
headers = dict(response.headers)
all_headers.append(headers)
print(f"Page {page} headers retrieved.")
except requests.exceptions.RequestException as e:
print(f"Error fetching headers: {e}")
return [] # Return empty list on error
return all_headers
if __name__ == '__main__':
test_url = "https://www.example.com"
headers_list = paginate_response_headers(test_url, max_pages=3)
if headers_list:
for i, headers in enumerate(headers_list):
print(f"\n--- Page {i+1} Headers ---")
for key, value in headers.items():
print(f"{key}: {value}")
else:
print("No headers retrieved.")
Add your comment