Home:ALL Converter>Excluding multiple terms from search results in Google Custom Search API

Excluding multiple terms from search results in Google Custom Search API

Ask Time:2018-06-08T21:58:02         Author:Nikki Luzader

Json Formatter

devKey = 'FAUX123456789'
customSearchEngineId = 'FAUX123456789'

searchTermArray = ['happy pets valencia CA',
                   'pet doctor z tuscon AZ',
                   'best friends veterinary hospital crossville TN',
                   'pet pal animal shelter st petersburg FL']

termsToExclude = ['happy','pet','vet']

numberOfResults = 1

for eachSearchTerm in searchTermArray:
    service = build("customsearch", "v1", developerKey=devKey)
    results = service.cse().list(q=eachSearchTerm, cx=customSearchEngineId, num=numberOfResults, excludeTerms=termsToExclude)
    results = results['items']
    print(results)

According to the google custom search API documentation, excludeTerms takes a string value. As you can see, I tried plugging in an array of strings and it doesn't quite work. I actually tested each term independently and doing so produced different results each time. (please excuse the dotcom as I was unable to post the actual links)

Heres the results for each test:

  • termsToExclude = ''
    happypetsveterinarydotcom/
    petdoctorxdotcom/
    bestfriendsvetdotorg/
    petpalanimalshelterdotcom/

    termsToExclude = ['happy','pet','vet']
    happypetsveterinarydotcom/
    ollinghillspetclinicdotcom/
    bestfriendsvetdotorg/
    petpalanimalshelterdotcom/

    termsToExclude = 'happy'
    krisersdotcom/location/valencia/
    valenciaanimalhospitaldotcom/reviews.html
    bestfriendsvetdotorg/
    petpalanimalshelterdotcom/adopt.php

    termsToExclude = 'pet'
    teambusbydotcom/real-estate-news/home-and-design/60-design-happy-pets-from-around-the-world-60-photos
    www.zmansiondotcom/
    www.bestfriendequinedotcom/
    disneyworld.disney.godotcom/entertainment/magic-kingdom/character-meet-goofy-donald/

    termsToExclude = 'vet'
    happypetsveterinarydotcom/medical-records/my-pets-medical-records/
    www.staystudio6dotcom/en/motels.az.tucson.6002.html
    langeanimalhospitaldotcom/josh-friends/
    petpalanimalshelterdotcom/event/purrfect-poses-yoga/

Now..

Google's vague user-friendly documentation states the following:

excludeTerms string : Identifies a word or phrase that should not appear in any documents in the search results.

I'm not sure what "any documents in the search results" means exactly, but what I have discovered through this process is that when a single string is used, it appears to exclude URLs with the string value, but when an array of strings is used, it does not seem to have the same behavior at all. Can anyone explain this? Or please explain if there is proper a way to plug in an array of terms in this keyword excludeTerms parameter?

I want to clarify though, that what I am trying to accomplish is the ability to plug in an array of strings so that my results will specifically exclude URLs containing the terms in termsToExclude, so that I can get more desirable URLs in my results. Also, remember that when I used individual strings, that produced the desired result, whereas the arrays did not seem to work the same way.

Thanks for any info!

Author:Nikki Luzader,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762300/excluding-multiple-terms-from-search-results-in-google-custom-search-api
yy