Skip to content

Instantly share code, notes, and snippets.

@mikevsky
Created June 12, 2018 07:03
Show Gist options
  • Save mikevsky/d817231e2c27bae6c119f3f59101b313 to your computer and use it in GitHub Desktop.
Save mikevsky/d817231e2c27bae6c119f3f59101b313 to your computer and use it in GitHub Desktop.
get_bing_pictures
Michael, [12 июня 2018 г., 10:00:33]:
def get_picture(self, search_request):
headers = {
'Ocp-Apim-Subscription-Key': self.bing_api_key,
}
params = {
'q': search_request,
'count': '10',
'offset': '0',
'mkt': 'en-us',
'safeSearch': 'Moderate',
}
response = requests.get(
self.bing_api_url,
params=params,
headers=headers
)
if response.status_code != 200:
response.raise_for_status()
try:
picture_url = response.json()['value'][randint(0, 7)]['thumbnailUrl']
except IndexError or KeyError:
try:
picture_url = response.json()['value'][0]['thumbnailUrl']
except IndexError or KeyError:
picture_url = None
return picture_url
bing_api_url = 'https://api.cognitive.microsoft.com/bing/v7.0/images/search?%s'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment