Reputation: 502
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
Reputation: 6438
It's useful to think of news-search as having 2 modes of operation:
Categorical/Trending. Format is:
https://api.cognitive.microsoft.com/bing/v5.0/news?category=FOO_CATEGORY&...&mkt=en-us&...
Query-based. Format is:
https://api.cognitive.microsoft.com/bing/v5.0/news/search?q=FOO_QUERY&...
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