HTTP request in python?

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)

I am trying to figure out why I am getting an error message of: Expecting value: line 1 column 1 (char 0)

There is no json to parse, you’re trying to parse json from a google search response that returns only text-based data (I suggest using bs4 to scrape data if that’s what you’re trying to do).

^^ Proof of no json data to parse.

1 Like

Try requesting to https://jsonplaceholder.typicode.com/users/, that should work

@Sky

Would using bs4 allow me to get the information on a specific search pulled from google?

All I am trying to do is test to see if what the user inputs in the console fits in a category in a game I am trying to build.

For example:

currentLetter = "A"
currentCategory = "Movies"
user_input = input("What is your word choice for the category " + currentCategory + "? (Make sure that is starts with the letter " + currentLetter +".")\n")
# I would need to test it here possibly with bs4

If the user put something in like: Avengers or Avatar I want to be able to test if it is a movie in the program without having to write the dictionary for this. I am wanting to do this so that if someone were to input: (something that is not the name of a movie) the program won’t count it as a correct answer.

Yes that is basically the objective of bs4.

1 Like

Okay, thanks for the tip. :relieved:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.