Automatically save images of your favorite characters from Google Image Search with Python

Introduction

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

environment

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


result

山.png

This time, we have nominated "Kei Shiragin". "Kaguya wants to tell you": "Sister heroine"

山.png

It's done. Very cute.

Kei-chan was the first nomination, so 3000 yen was taken as a nomination fee (what a mess)

from now on

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

Automatically save images of your favorite characters from Google Image Search with Python
Save dog images from Google image search
[Python] Download original images from Google Image Search
Search and save images of Chino Kafu from Twitter
Send image with python, save with php
I tried to automatically collect images of Kanna Hashimoto with Python! !!
[Python] Try to recognize characters from images with OpenCV and pyocr
Basics of binarized image processing with Python
A Python script that automatically collects typical images using bing image search
Create wordcloud from your tweet with python3
Get tweets with Google Cloud Function and automatically save images to Google Photos
Image acquisition from camera with Python + OpenCV
Drawing with Matrix-Reinventor of Python Image Processing-
Upload images to Google Drive with Python
Automatically search and download YouTube videos with Python
Color page judgment of scanned image with python
Image processing from scratch with python (5) Fourier transform
Categorize face images of anime characters with Chainer
Image processing from scratch with python (4) Contour extraction
Get Google Image Search images in original size
Create an image with characters in python (Japanese)
Bulk download images from specific URLs with python
Extract images and tables from pdf with python to reduce the burden of reporting
[Latest] How to use Python library to save Google image search & use Chrome Driver on ubuntu
Flow of creating your own package with setup.py with python
Extract the table of image files with OneDrive & Python
Bulk download images from specific site URLs with python
Learn Nim with Python (from the beginning of the year).
How to scrape image data from flickr with python
Get the image of "Suzu Hirose" by Google image search.
[OpenCV / Python] I tried image analysis of cells with OpenCV
Try projective transformation of images using OpenCV with Python
Read QR code from image file with Python (Mac)
Save screenshot of [Python] [Windows] screen as an image
[Basics of data science] Collecting data from RSS with python
I tried "morphology conversion" of images with Python + OpenCV
Call the API of Hatena Blog from Python and save your blog articles individually on your PC
Sequential search with Python
Scraping google search (image)
Segfault Python with 33 characters
Binary search with python
Binary search with Python3
Image processing with Python
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
Extract template of EML file saved from Thunderbird with python3.7
Automatically organized mp3s downloaded from Google Play Music using Python
Collect large numbers of images using Bing's image search API
Run the intellisense of your own python library with VScode.
Image collection by calling Bing Image Search API v5 from Python
Get data from analytics API with Google API Client for python
Save images on the web to Drive with Python (Colab)
Google search for the last line of the file in Python
[Python] Easy reading of serial number image files with OpenCV
Python: Extract file information from shared drive with Google Drive API
[Python] Based on your favorite desktop image, let's automatically create a terminal / Vim / VS Code theme that suits your feelings with one command.