Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reserved words in Google Custom Search API

I am working with the Google Search API, and I am running into some trouble. This request (in Python, using the requests library) works fine

res = requests.get("https://www.googleapis.com/customsearch/v1", params={
    "cx": <key1>,
    "key": <key2>,
    "alt": "json",
    "num": 2,
    "q": "cat sock ship hero monkey baby match"
})

and returns results with the syntax according to the documentation

However, this request does not work:

res = requests.get("https://www.googleapis.com/customsearch/v1", params={
    "cx": <key1>,
    "key": <key2>,
    "alt": "json",
    "num": 2,
    "q": "cat sock ship hero monkey footnoteref baby match"
})

it returns this:

{'kind': 'customsearch#search',
 'queries': {'request': [{'count': 2,
    'cx': '<key>',
    'inputEncoding': 'utf8',
    'outputEncoding': 'utf8',
    'safe': 'off',
    'searchTerms': 'cat sock ship hero monkey baby footnoteref match',
    'title': 'Google Custom Search - cat sock ship hero monkey baby footnoteref match',
    'totalResults': '0'}]},
 'searchInformation': {'formattedSearchTime': '0.22',
  'formattedTotalResults': '0',
  'searchTime': 0.218722,
  'totalResults': '0'},
 'spelling': {'correctedQuery': 'cat sock ship hero monkey baby footnote ref match',
  'htmlCorrectedQuery': 'cat sock ship hero monkey baby <b><i>footnote ref</i></b> match'},
 'url': {'template': 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json',
  'type': 'application/json'}}

The only difference between the two queries is that the latter has the word "footnoteref" in it. I did not find in the documentation anything about this word and its impact on the API's behavior. What is happening? Is there a way to disable this behavior, or a list of reserved words? For now, I am just going to remove the offending word from the query, but I am afraid I am going to play a whack-a-mole game of removing words each time other offending word pops out.

like image 835
user4052054 Avatar asked Nov 15 '17 12:11

user4052054


People also ask

How do I customize Google Custom Search?

From the Programmable Search Engine homepage, click Create a custom search engine or New search engine. In the Sites to search box, type one or more sites you want to include in the search results. You can include any sites on the web, even sites you don't own. Don't worry, you can always add more later.

What is CX in Google Custom Search?

Create Programmable Search Engine Once it is created, you can find the engine's ID in the Setup > Basics > Search Engine ID section of the Control Panel. This is the cx parameter used by the API.

How do I use Google Custom Search API?

GET YOUR GOOGLE SEARCH API KEY Accessing Google's Custom Search JSON API requires the use of an API key. To get this key, navigate to the Custom Search JSON API page and click Get a Key. Choose an existing project, or create a new one, and click Next. Note and copy your API key.


1 Answers

I searched on google for both "cat sock ship hero monkey footnoteref baby match" and "cat sock ship hero monkey baby match".

You said that "cat sock ship hero monkey footnoteref baby match" doesn't return anything, and that's because Google actually suggests a different search: 'cat sock ship hero monkey baby footnote ref match'.

When you don't have results, you should remove a word from the search (I will start with the last word) and try again. Or you should just try with the suggested search, like: 'cat sock ship hero monkey baby footnote ref match'.

The search works fast, I suggest you to implement the following technique:

  • a) Your search contains less than 3-4 words. You should repeat the search but add a new word from google's 'correctedQuery' suggestion.
  • b) Your search contains more than 4 words. You should remove the last word or a "link word" like "for", "and".. and repeat the search.

Good luck.

like image 98
Pascut Avatar answered Oct 02 '22 18:10

Pascut