Ruthus99
Ruthus99

Reputation: 502

How do you structure a Bing Cogntivie Services API News request?

I am currently writing an application that uses the Bing Cognitive Services search API and I am trying to scrape the news results in particular and am running into some problems

For a normal search, the request is structured pretty simply:

def bing_search(query):

    url = 'https://api.cognitive.microsoft.com/bing/v5.0/search'

    payload = {'q': query, 'freshness': 'week', 'mkt': 'en-us'}

    headers = {'Ocp-Apim-Subscription-Key': 'API KEY', 'X-MSEdge-ClientID': ''}

    r = requests.get(url, params=payload, headers=headers)

    return r.json()

This works perfectly and returns the correct results when I add a query in, however thats just for normal search.

When I try and use the news search, and I change the url variable to this as the documentation suggests:

https://api.cognitive.microsoft.com/bing/v5.0/news?

However when I run this and insert a query, it returns a json which contains only the top news stories of the day, and not relevant at all to the query I added.

Am I structuring the url correctly? I would be very grateful if anyone could help me out to structure the request so it returns the correct results.

Thanks :)

Upvotes: 0

Views: 146

Answers (1)

Rob Truxal
Rob Truxal

Reputation: 6438

It's useful to think of news-search as having 2 modes of operation:

To enter a q=... param in a news search you'll need to use that second "Query-based" format.

If you want to do a categorical search, you'll need to specify either en-US or en-GB in the mkt param, then replace FOO_CATEGORY with one of the following:

NEWS_CATEGORIES_US = (
    'Business',
    'Entertainment',
    'Entertainment_MovieAndTV',
    'Entertainment_Music',
    'Health',
    'Politics',
    'ScienceAndTechnology',
    'Science',
    'Technology',
    'Sports',
    'Sports_Golf',
    'Sports_MLB',
    'Sports_NBA',
    'Sports_NFL',
    'Sports_NHL',
    'Sports_Soccer',
    'Sports_Tennis',
    'Sports_CFB',
    'Sports_CBB',
    'US',
    'US_Northeast',
    'US_South',
    'US_Midwest',
    'US_West',
    'World',
    'World_Africa',
    'World_Americas',
    'World_Asia',
    'World_Europe',
    'World_MiddleEast',
)
NEWS_CATEGORIES_GB = (
    'Business',
    'Entertainment',
    'Health',
    'Politics',
    'ScienceAndTechnology',
    'Sports',
    'UK',
    'World',
)

If you're still having trouble, here's a simple python 2.7 interface: https://github.com/rtruxal/bingapipy

Upvotes: 1

Related Questions