如何从python中的google搜索中排除某些网站?

2024-04-20 10:48:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Mario Vilas的Google搜索API,可以在github上找到:https://github.com/MarioVilas/googlesearch

现在,在搜索过程中,我想从显示中删除某些站点。我已经阅读了文档,似乎没有任何东西允许我们排除某些域。这方面有什么办法吗?如果不是的话,你知道有没有其他谷歌搜索API可以做到这一点

这是我的密码:

keyword = input("Keyword: ")
country = input("Country:")
tld_of_country = domain_names[country]



for website in search(keyword, tld=tld_of_country, num=2, 
stop=2, country="canada", pause=2): 
 try:
      links.append(website)
 except:
      continue

Tags: ofhttpsgithubcomapiinputgooglewebsite
1条回答
网友
1楼 · 发布于 2024-04-20 10:48:11

https://support.google.com/gsa/answer/2672318?hl=en

搜索查询长度有限,因此如果使用以下方法排除太多域: “-website:site”,谷歌不会返回任何结果。在这种情况下,您可以使用正则表达式或类似的方法手动从列表中排除。您可以使用:

[x for x in yourlist if "domain" not in x]

或者,在您的情况下,可以在追加过程之前添加if语句

相关问题 更多 >