This Python code is a script that sends an HTTP GET request to a URL (in this case, 'https://google.com') using the requests
library and saves the response data to a file. Let's go through the code step by step:
-
Import required libraries:
json
: Used to handle JSON data.requests
: Used to make HTTP requests.
-
Define the URL and headers for the GET request:
url
: The URL to which the GET request is sent. In this example, it is 'https://google.com'.headers
: HTTP headers that can be added to the request if needed. Currently, it's an empty dictionary.
-
Send the GET request:
requests.get(url, headers=headers)
: Sends a GET request to the specified URL with optional headers.
-
Check the response status code:
response.status_code
: Contains the HTTP status code returned by the server (e.g., 200 for success, 404 for not found, etc.).
-
Process the response:
- If the response status code is 200 (OK), it proceeds to process the response data.
- If the 'Content-Type' header of the response contains 'json', it assumes that the response contains JSON data and attempts to decode it using
response.json()
. - If the JSON decoding is successful, it saves the JSON data to a file with an extension of '.json'.
- If JSON decoding fails or the 'Content-Type' header does not contain 'json', it saves the response content (non-JSON data) to a file with a default extension of '.txt'.
-
Save the response data to a file:
- The data is saved to a file named 'response_data' with the appropriate extension (either '.json' or '.txt') based on the content type.
-
Print the outcome:
- If the request is successful and the response is saved, it prints the file name and extension where the data is saved.
- If the request is not successful (e.g., the status code is not 200), it prints an error message along with the status code.
This script can be used to make a GET request to any URL and save the response data to a file, provided that the server responds with either JSON or non-JSON content. Note that this script does not handle cases where the server returns other types of data (e.g., images, videos, etc.).
import json
import requests
url = 'https://google.com'
headers = {}
response = requests.get(url, headers=headers)
response_headers = response.headers['Content-Type']
if response.status_code == 200:
if 'json' in response_headers:
filetype = 'json'
try:
content = response.json()
except json.JSONDecodeError:
print("Error: Response content is not in JSON format.")
content = None
else:
filetype = 'txt' # You can set a default extension for other content types.
content = response.content
filename = "response_data"
with open(f"{filename}.{filetype}", 'w', encoding='utf-8') as f:
if content is not None:
if filetype == 'json':
json.dump(content, f, ensure_ascii=False, indent=4)
else:
f.write(content)
print(f"Response saved to {filename}.{filetype}")
else:
print(f"Failed to fetch data. Status Code: {response.status_code}")