-
Notifications
You must be signed in to change notification settings - Fork 0
/
bingimagescraper.py
35 lines (31 loc) · 1.25 KB
/
bingimagescraper.py
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
import requests
from bs4 import BeautifulSoup
def image_scraper(query):
"""scrapes user search term for all images via bing image search
:param http url ex. https://www.cookinglight.com
:return dictionary key:alt text; value: source link"""
adlt = 'safe'
search = query.strip()
search = search.replace(' ', '+')
url = 'https://bing.com/images/search?q=' + search + '&safeSearch=' + adlt
print("\nsearch url:", url, "\n")
# A user agent is a computer program representing a person; https://developer.mozilla.org/en-US/docs/Glossary/User_agent
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"
headers = {"user-agent": USER_AGENT}
resp = requests.get(url, headers=headers)
soup = BeautifulSoup(resp.content, "html.parser")
image_dict = {}
# finds image links
images = soup.find_all('a', class_='iusc')
for i in images:
try:
img_url = eval(i['m'])['murl']
img_title = eval(i['m'])['desc']
image_dict[img_title] = img_url
except:
pass
return image_dict
if __name__ == "__main__":
query = input("Enter a search term for your image: ")
results = image_scraper(query)
print(results)