You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中使用Mario Vilas谷歌搜索API排除特定网站?

Solution for Excluding Specific Domains in Google Search Results

Option 1: Filter Results After Fetching

Since the Mario Vilas googlesearch library doesn’t have native domain exclusion, you can add a simple post-fetch filter to skip unwanted domains. Here’s how to tweak your existing code:

First, define a list of domains you want to block. Then, check each result against this list before adding it to your links collection.

Modified code example:

# List of domains you want to exclude
blocked_domains = ["example.com", "unwanted-site.net"]

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

for website in search(keyword, tld=tld_of_country, num=2, stop=2, country="canada", pause=2):
    # Skip results from blocked domains
    if not any(domain in website for domain in blocked_domains):
        try:
            links.append(website)
        except:
            continue

For stricter domain matching (e.g., avoiding partial matches), you can parse the URL to extract the exact domain using urllib.parse—this ensures you don’t accidentally exclude sites that mention the blocked domain in their path.

Option 2: Use an API with Native Domain Exclusion

If you want a more efficient solution without post-processing, consider these alternatives:

  • SerpAPI: Offers a dedicated exclude parameter where you can list domains to block directly in your search request. It handles filtering on the backend, so you get clean results immediately.
  • Google Custom Search API: Lets you use siteSearchFilter: "-" alongside siteSearch to exclude specific domains. You’ll need to set up an API key and custom search engine, but it’s an official, reliable option.

Both tools let you exclude domains as part of the search query, which is more efficient than filtering results after they’re fetched.

内容的提问来源于stack exchange,提问作者ImNotARobot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 18:57:42