File size: 1,510 Bytes
5f685fd |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import json
import requests
import re
import urllib.parse
def _extractBingImages(html):
pattern = r'mediaurl=(.*?)&.*?expw=(\d+).*?exph=(\d+)'
matches = re.findall(pattern, html)
result = []
for match in matches:
url, width, height = match
if url.endswith('.jpg') or url.endswith('.png') or url.endswith('.jpeg'):
result.append({'url': urllib.parse.unquote(url), 'width': int(width), 'height': int(height)})
return result
def _extractGoogleImages(html):
images = []
regex = re.compile(r"AF_initDataCallback\({key: 'ds:1', hash: '2', data:(.*?), sideChannel: {}}\);")
match = regex.search(html)
if match:
dz = json.loads(match.group(1))
for c in dz[56][1][0][0][1][0]:
try:
thing = list(c[0][0].values())[0]
images.append(thing[1][3])
except:
pass
return images
def getBingImages(query, retries=5):
query = query.replace(" ", "+")
images = []
tries = 0
while(len(images) == 0 and tries < retries):
response = requests.get(f"https://www.bing.com/images/search?q={query}&first=1")
if(response.status_code == 200):
images = _extractBingImages(response.text)
else:
print("Error While making bing image searches", response.text)
raise Exception("Error While making bing image searches")
if(images):
return images
raise Exception("Error While making bing image searches")
|