EDIT
Ok, so here is my whole code so far:
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import requests
import time
import os
import Tkinter as tk
def get_page():
global driver
driver = webdriver.Chrome()
driver.get(url)
last_height = driver.execute_script('return
document.body.scrollHeight')
while True:
driver.execute_script('window.scrollTo(0,
document.body.scrollHeight);')
new_height = driver.execute_script('return
document.body.scrollHeight')
if new_height == last_height:
break
else:
last_height = new_height
#This function uses BeautifulSoup to parse through the page source and find images.
def get_img():
sp = bs(driver.page_source, 'html.parser')
for image in sp.find_all('img'):
images.append(image)
#Create folder which will contain downloaded images.
def make_dir():
if not os.path.exists('Downloaded images'):
os.mkdir('Downloaded images')
os.chdir('Downloaded images')
#Function which saves images.
def save_img():
x = 0
for image in images:
try:
url = image['src']
source = requests.get(url)
with open('img-{}.jpg'.format(x), 'wb') as f:
f.write(requests.get(url).content)
x += 1
except:
print 'Error while saving image.'
root = tk.Tk()
root.title('Image Scraper 1.0')
tk.Label(root, text = 'Enter URL:').grid(row=0)
e1 = tk.Entry(root)
e1.grid(row=0, column=1)
e1.insert(driver.get(url))
button1 = tk.Button(root, text = 'SCRAPE', command =scrape_site).grid(row=3, column=1, sticky=tk.W, pady=4)
button1.pack()
root.mainloop()
I tried to put the whole scrape_site function in tkinters button command=, which is stupid i see this now, and obviously it doesn't work. As you can see, I copied whole tkinter code to main scraper file. Any thoughts? I will appreciate any input :)
I recently posted my question about web scraper, which downloaded images of cats. This time I decided, that I will make another step forward. I want to make GUI web scraper which will download images from website which user will input in tkinter Entry widget. Is this even possible? I also created two .py files: one for script of scraper and second one for gui. Can it be stored this way or should it be one file? Here is the scraper code which opens and scrolls page (using selenium), it works fine. My only question is: how to put it in tkinter? :)
def get_page():
global driver
driver = webdriver.Chrome()
driver.get(url)
last_height = driver.execute_script('return document.body.scrollHeight')
while True:
driver.execute_script('window.scrollTo(0,
document.body.scrollHeight);')
new_height = driver.execute_script('return
document.body.scrollHeight')
if new_height == last_height:
break
else:
last_height = new_height
get_page()