How to exclude particular websites from Bing web search API result?

by declan_ritchie , in category: Local SEO , a year ago

How to exclude particular websites from Bing web search API result?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

by chasity.halvorson , a year ago

@declan_ritchie 

To exclude particular websites from Bing web search API results, you can use the "NOT" operator in your search query to exclude specific domains or websites from the results.


For example, if you want to search for "best restaurants" but exclude results from Yelp, you can use the following query:


best restaurants NOT site:yelp.com


This will exclude any results from the Yelp website. You can replace "yelp.com" with any other website that you want to exclude.


Alternatively, you can also use the "-site:" operator to achieve the same result. For example:


best restaurants -site:yelp.com


This will also exclude any results from Yelp.


Note that this technique will only work for excluding results from specific domains or websites. It will not work for excluding specific pages or sections within a website.

by chasity.halvorson , 4 months ago

@declan_ritchie 

To exclude particular websites from Bing web search API result using the WebSearchRequest object in the Bing web search API, you can utilize the q parameter with the -site: operator and the website you wish to exclude. Here's an example in Python:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import http.client, urllib.parse, json

# Replace with your subscription key and endpoint
subscription_key = 'Your-Subscription-Key'
host = 'api.bing.microsoft.com'
path = '/v7.0/search'

# Replace with your user query and excluded website
query = 'best restaurants -site:yelp.com'

# Construct the request URL
params = '?q=' + urllib.parse.quote(query) + '&count=10&offset=0&mkt=en-US&safesearch=Moderate'
headers = {'Ocp-Apim-Subscription-Key': subscription_key}

try:
    conn = http.client.HTTPSConnection(host)
    conn.request("GET", path + params, headers=headers)
    response = conn.getresponse()
    data = response.read().decode('utf-8')
    results = json.loads(data)

    # Parse and print the results
    for result in results['webPages']['value']:
        print(result['name'])
        print(result['url'])

    conn.close()
except Exception as e:
    print(f"Error: {e}")


Make sure to replace 'Your-Subscription-Key' with your actual Bing search API subscription key, and 'best restaurants -site:yelp.com' with your desired search query and excluded website.


With this approach, the API will exclude results from the specified website from the search results.

by laverna_hirthe , 4 months ago

@declan_ritchie 

Please note that the code provided is an example in Python and should be modified to fit your specific programming language and environment.