【问题标题】:Excluding multiple terms from search results in Google Custom Search API从 Google Custom Search API 的搜索结果中排除多个字词
【发布时间】:2018-11-18 14:42:18
【问题描述】:
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)

根据google custom search API documentationexcludeTerms 采用字符串值。如您所见,我尝试插入一个字符串数组,但效果不佳。我实际上独立地测试了每个术语,并且每次都会产生不同的结果。 (请原谅 dotcom,因为我无法发布实际链接)

以下是每个测试的结果:

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

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

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

    termsToExclude = 'pet'
    teambusbydotcom/real-estate-news/home-and-design/60-design-happy-pets-from-环游世界 60 张照片
    www.zmansiondotcom/
    www.bestfriendequinedotcom/
    disneyworld.disney.godotcom/entertainment/magic-kingdom/character-meet-傻傻的唐纳德/

    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/

现在..

Google 的 vague user-friendly documentation 声明如下:

excludeTerms string :标识不应出现在搜索结果的任何文档中的单词或短语。

我不确定 “搜索结果中的任何文档” 的确切含义,但我通过此过程发现的是,当使用单个字符串时,它似乎会排除带有字符串值,但是当使用字符串数组时,它似乎根本没有相同的行为。谁能解释一下?或者请解释是否有适当的方法可以在此关键字excludeTerms 参数中插入一组术语?

我想澄清一下,我想要完成的是插入字符串数组的能力,以便我的结果将明确排除包含termsToExclude 中的术语的 URL,以便我可以获得更理想的 URL在我的结果中。另外,请记住,当我使用单个字符串时,会产生所需的结果,而数组的工作方式似乎不同。

感谢您提供任何信息!

【问题讨论】:

    标签: python python-3.x google-custom-search google-api-python-client


    【解决方案1】:

    excludeTerms 是一个扁平字符串,所以不确定你会在数组中传递什么行为。

    尝试一下

    termsToExclude = 'happy pet vet'
    

    【讨论】:

    • 完美解决方案!这是我想太多的经典案例。文档中没有关于排除多个单词的内容。这应该是显而易见的吗?我不能赞成你的解决方案,但我可以肯定地说这解决了我的问题!非常感谢=)
    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2020-11-07
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多