Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0931979

Browse files
added python code file
1 parent 482d48c commit 0931979

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

‎Image-scrape-download/image.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Import Module
2+
from tkinter import *
3+
from tkhtmlview import HTMLLabel
4+
from tkinter import ttk
5+
from tkinter import font as tkFont
6+
import requests
7+
from bs4 import BeautifulSoup
8+
import urllib3
9+
import shutil
10+
11+
# Function to save image to the users file system
12+
def saveImage():
13+
file_name = search_box.get().replace(" ","")
14+
image_file = getImage()
15+
http = urllib3.PoolManager()
16+
with open(file_name, 'wb') as out:
17+
r = http.request('GET', image_file, preload_content=False)
18+
shutil.copyfileobj(r, out)
19+
20+
# Function to scrape image source from bing
21+
def getImage():
22+
search = search_box.get()
23+
url = "https://www.bing.com/images/search?q={}".format(search.replace(' ','?'))
24+
page = requests.get(url)
25+
26+
# Start scraping resultant html data
27+
soup = BeautifulSoup(page.content, 'html.parser')
28+
images_div = soup.find('div',{'id':'mmComponent_images_2'})
29+
images_ul = images_div.find('ul')
30+
31+
image = images_ul.find("li")
32+
link = image.find('img').get('src')
33+
34+
return link
35+
36+
# Function to show image in the GUI
37+
def showImage():
38+
link = getImage()
39+
str_value = '<img src="{}">'.format(link)
40+
my_label.set_html(str_value)
41+
42+
43+
# Create tkinter Object
44+
root = Tk()
45+
46+
# Set Geomerty of window
47+
root.geometry("400x500")
48+
root.title("Image viewer and downloader")
49+
50+
# Set styles
51+
style = ttk.Style()
52+
style.theme_use('alt')
53+
style.map('my.TButton', background=[('active','white')])
54+
style.configure('my.TButton', font=('Helvetica', 16, 'bold'))
55+
style.configure('my.TButton', background='white')
56+
style.configure('my.TButton', foreground='orange')
57+
style.configure('my.TFrame', background='white')
58+
59+
# Add labels and buttons
60+
my_label = HTMLLabel(root)
61+
my_label.pack(pady=20, padx=20)
62+
63+
search_box = Entry(root, font=("Helvetica 15"), bd = 2, width=60)
64+
search_box.pack(side = TOP, pady=5, padx=15, ipadx=5)
65+
66+
search_btn = ttk.Button(text="Scrape Image!",command=showImage,style='my.TButton')
67+
search_btn.pack(side=TOP)
68+
69+
save_btn = ttk.Button(text="Download Image!",command=saveImage,style='my.TButton')
70+
save_btn.pack(side=TOP)
71+
72+
# Execute Tkinter
73+
root.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /