I wrote a code to save 20 Google image search results in order from the top, referring to the following article. It's not a big deal Google image search with Python and save images in folder
OS:Windows 10 home Language: python 3.8.1
Code I will explain the code in the comments
download_images.py
# install module
import requests
import random
import shutil
import bs4
#Get URL to save
def image(data,num):
#Get URL for Google Image Search
res = requests.get("https://www.google.com/search?hl=jp&q=" + data + "&btnG=Google+Search&tbs=0&safe=off&tbm=isch")
html = res.text #Text conversion
soup = bs4.BeautifulSoup(html,'lxml') #Plastic surgery
links = soup.find_all("img") #Get img element
link = links[num].get("src") #Get numth srcURL
return link
#Download from the corresponding URL
def download(url,file_name):
req = requests.get(url, stream=True)
if req.status_code == 200:
with open(file_name + ".png ", 'wb') as f: #Export png to file with bin
req.raw.decode_content = True
shutil.copyfileobj(req.raw, f) #Copy png image data to file
#Pick up the name of the child to search for
name = input("What child are you looking for?:")
for i in range(1,20): #20 sheets for convenience
link = image(name,i)
download(link,name + str(i))
print(link)
i += 1 #Repeat 20 times
This time, we have nominated "Kei Shiragin". "Kaguya wants to tell you": "Sister heroine"
It's done. Very cute.
Kei-chan was the first nomination, so 3000 yen was taken as a nomination fee (what a mess)
I'm very happy with the feeling of surrounding a beautiful girl, but since the source is Google image search, the pixel is small. Also, since the number of src that can be read at one time is more than 20, it is not possible to download about 100 at once (I think it can be improved).
--Then, have a good 2D life
Recommended Posts