Skip to content
Advertisement

Problem with searching multiple keywords using google custom search API

I am trying to search for multiple keywords (in the list of filteredList) and get a list of each search result. This is the code I have tried below:

JavaScript

Running the answer script

The error code I got (Running the answer script):

JavaScript

Advertisement

Answer

I don’t have API keys to run this code but I see few mistakes:

When you use

JavaScript

then you get word from list, not its index so you can’t compare it with number.

To get number you would use

JavaScript

But instead of this version better use first version but then use items instead of filterd[items] in

JavaScript

If you choose version with range(len(filteredList)): then don’t add 1 to items – because then you get numbers 1..6 instead of 0..5 so you skip first element filteredList[0] and it doesn’t search first word. And later you try to get filteredList[6] which doesn’t exist on list and you get your error message.

JavaScript

BTW: you have to create newDict = dict() in every loop.


BTW: standard print() and pprint.pprint() is used only to sends text on screen and always returns None so you can’t assign displayed text to variable. If you have to format text then use string formatting for this.


EDIT: version with range(len(...)) which is not preferred in Python.

JavaScript

EDIT:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement