Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this assignment you program a ( console ) Python program which sends search requests to Google and which displays the results of those requests
In this assignment you program a console Python program which sends search requests to Google and which displays the results of those requests as well as the returned HTTP response status and headers.
Preliminaries
Acquiring Access to Google Custom Search
A simple Google search typically issued through a web browser uses the following URL example: search for 'dandelion':
https:wwwgoogle.comsearchqdandelion
where:
qdandelion specifies the search query.
This type of search, however, returns HTML to see the HTML returned by the Web server, view the 'page source' in your browser and since HTML is not a good format for machines to consume, Google also exposes a socalled custom search JSON service which accepts standard HTTP GET search queries and which returns the results as JSON example: search for 'dandelion':
https:customsearchgoogleapis.comcustomsearchvkey&cx&qdandelion
where:
key specifies a userspecific search API 'key'. must be requested from Google
cx specifies a userspecific custom search engine IDmust be requested from Google
qdandelion specifies the search query.
Note: The two bullet items below contain links for requesting an API key and a search engine ID from Google both free of charge Since OSU has very recently changed its relationship with Google, you cannot use your ONID login for requesting them. Instead, use your private Google gmail account. If you do not yet have such an account, get yourself one.
Link for requesting a search API key from Google store the key in a safe place
Link for requesting a search engine ID from Google store the ID in a safe place
The API key and search engine ID which are user specific allow for freeofcharge searches per day.
Reading Search Results
Upon a successful search, Google returns a JSON data structure containing, among lots of other things, up to search results subsequent results can be requested, but each such request counts against the quotum of day For this assignment we will only use those first search results returned
The part of the search results that we are interested in are stored in a JSON list called items We strongly recommend that you first do a custom search using your browser, so that you can see and study the returned results
Requirements
Since both the Google API key and the Google search engine ID are userspecific, we do not want to storehardwire them in our program if we would do that, then everyone who would have our program could use our individual key and search engine ID Instead, we will store them in a file and have the program read them from the file.
The JSON format of the file must be as follows:
"key" :
"searchengineid :
where and must be replaced with the API key and the search engine ID respectively.
IMPORTANT: note that the content of the file represents a JSON structure!!! Again refer to the PythonJSON lab for how to extract data from JSON structures with Python.
In order to provide some flexibility in the naming and location of the file, we will not hardwire the file name and file location in the program, but instead pass it as a parameter on the commandline when running the program. For instance, if the program is called googlesearch.py and the file with the key and search engine ID is called key.json and is located in the folder c:temp we will call the program as follows:
python googlesearch.py c:tempkeyjson
Refer to the PythonRefresher lab on how to read commandline arguments.
The program must prompt the user for a search term and print out the results of the search as follows:
First: HTTP response status code
Second: HTTP response headers
Third: For each of the returned items: title and link printed as follows:
title link
The program keeps prompting the user for search terms until the user supplies the search term stop.
The following is an example session of the program running:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started