Issue
We made a program which accepts an input through a tkinter GUI and goes to google images,and downloads images based on the input.Here is the code:
import requests
import bs4
import random
from PIL import Image
from tkinter import messagebox as msgbox
i=0
import os
from tkinter import *
from tkinter import filedialog
ac=str(random.randint(1,20))
b=str(random.randint(20,38))
y=Tk()
def find_file():
aaa=filedialog.askdirectory()
return aaa
def create_folder():
ad=find_file()
global ac
global b
ads=os.path.join(ad,f"Img{ac}{b}")
os.mkdir(ads)
return ads
defe=Entry(bg="white")
defe.grid(row=2,column=2)
adj=Label(text="Enter the name of the photo(s) you want to download :")
adj.grid(row=2,column=1)
ack=Label(text="How many photos you want to download?")
ack.grid(row=3,column=1)
dee=Entry(bg="white")
dee.grid(row=3,column=2)
def download_images():
defei=defe.get()
deee=int(dee.get())
aadgc=[]
play=True
if " gif" in defei or ".gif" in defei:
msgbox.showerror("GIF not supported",".gif format is not supported by this software.Sorry for the inconvenience")
play=False
while play:
asd=create_folder()
for start in range(0,400,20):
bararara=f"https://www.google.co.in/search?q={defei}&source=lnms&tbm=isch&start={start}#imgrc=fTslNdnf0RRRxM"
a=requests.get(bararara).text
soup=bs4.BeautifulSoup(a,"lxml")
ab=soup.find_all("img",{"class":"n3VNCb"},limit=deee)
aadgc.extend(ab)
aa=[abb["src"] for abb in aadgc]
for source in aa:
r=random.randint(0,100)
ra=random.randint(0,1000)
raa=asd+"\\"+str(r)+str(ra)+".png"
try:
binary=requests.get(source).content
except requests.exceptions.MissingSchema:
binary=requests.get("http:"+source).content
except:
binary=requests.get("https:"+source).content
with open(raa,"wb") as saaho:
saaho.write(binary)
saaho.close()
global i
i+=1
if i==int(deee):
break
asd=asd.replace("/","\\")
os.system(f"explorer \"{asd}\"")
break
aadg=Button(y,bg="red",text="Download!",command=lambda:download_images(),activebackground="dark red",activeforeground="grey")
aadg.grid(row=4,column=1)
y.mainloop()
aadg=Button(y,bg="red",text="Download!",command=lambda:download_images(),activebackground="dark red",activeforeground="grey")
aadg.grid(row=4,column=1)
y.mainloop()
But we are getting the thumbnail of the image rather than the image,because of which the software returns only low-resolution photos and doesn't support .gif images.
Also we are not able to find the class that the main image belongs to. Thanks.
Solution
With Selenium:
Click an image from search results.
Wait until the image is visible.
image_link = driver.find_element_by_css_selector(".tvh9oe.BIB1wf .eHAdSb>img").get_attribute("src")
You can use the same locator for bs4
Answered By - vitaliis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.