I am trying to use bing API to pull news in my python code. No matter what parameters I have tried I still get data for last 2-3 days. My sample code is given below. I have tried freshness and other parameters in latest documentation but no luck.
import requests
import urllib.parse
search_term = "Microsoft"
search_term = urllib.parse.quote(search_term)
# API endpoint
search_url = "https://api.bing.microsoft.com/v7.0/news"
headers = {"Ocp-Apim-Subscription-Key": "XXXXX"}
start_date = "2021-01-01"
end_date = "2022-10-01"
date_range = f"{start_date}..{end_date}"
params = {"q": search_term, "freshness": date_range, "mkt": "en-US"}
# Add try/except for error handling
try:
response = requests.get(search_url, headers=headers, params=params)
data = response.json()
for article in data["value"]:
# Print first word and date only
first_word = article["name"].split()[0]
date = article["datePublished"][:10]
print(first_word, date)
#print(response.status_code)
print(response.text)
except Exception as e:
print("Error querying API:", e)