在 Google 自定义搜索 API 中从搜索结果中排除多个字词


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)

根据谷歌自定义搜索API文档,excludeTerms采用字符串值。如您所见,我尝试插入字符串数组,但它不太有效。我实际上独立测试了每个术语,每次都产生了不同的结果。(请原谅互联网,因为我无法发布实际链接(

以下是每个测试的结果:

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






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

    termsToExclude ='happy'
    krisersdotcom/地点/瓦伦西亚/
    valenciaanimalhospitaldotcom/reviews.html
    最好的朋友Vetdotorg/
    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/魔法王国/角色遇见高飞唐纳德/术语排除 ='vet'
    快乐宠物兽医网络/医疗记录/我的宠物医疗记录/www.staystudio6dotcom/en/motels.az.tucson.6002.html
    langeanimalhospitaldotcom/josh-friends/petpalanimalshelterdotcom/event/purrfect-poses-yoga/



现在。。

谷歌模糊的用户友好文档指出以下内容:

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

我不确定"搜索结果中的任何文档">到底是什么意思,但我通过这个过程发现的是,当使用单个字符串时,它似乎排除了带有字符串值的 URL,但是当使用字符串数组时,它似乎根本没有相同的行为。谁能解释一下?或者请解释是否有适当的方法在此关键字excludeTerms参数中插入术语数组?

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

感谢您提供任何信息!

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

试试看

termsToExclude = 'happy pet vet'

最新更新