Question: How can I use the request() function to make an HTTP request in python without getting an error response?
Repl link:https://replit.com/@SoullessG/Test1-For-Scatorgories
#I am using this code:
import requests
import json
from os import system as sig
def CL(): sig("clear")
# Take in user input for the search term
search_term = input("What is your word choice? (Make sure that it starts with the correct letter!) ")
# Create the HTTP GET request URL for Google search API with the search term
google_search_url = "https://www.google.com/search?q={}&".format(search_term)
# Make the HTTP GET request to Google search API
google_search_response = requests.get(google_search_url)
# Try to parse the JSON with a try-except block in case there's an error
google_search_json = None
try:
google_search_json = google_search_response.json()
except Exception as e:
CL()
Made = google_search_response.text;
with open("myfile.txt", "w+") as f:f.write(Made)
print("Error parsing JSON:", e)
# If no error occurred, check if the response was successful and if the JSON could be parsed
if google_search_response.status_code == 200 and google_search_json is not None:
# Extract the first search result
first_result = google_search_json["items"][0]["snippet"]
# Print the title of the first search result
print("Title: " + first_result)
else:
print(google_search_response.status_code)
print(google_search_json)