Batch download images from a specific URL with python Modified version

Introduction

In "Download images from a specific URL in bulk with python" that I wrote as a modified version before, for example This time, I fixed the problem that the image could not be downloaded due to ERROR.

Edited code

I was only judging if there was "../", but I added it because I wasn't thinking about when there was no domain. Judgment solved the problem by downloading as it is if there is "http: //" or "https: //", or by prefixing the URL that was entered first.

Before editing


    for j in range(0,(len_url-1)):
        url = number_url[j]
        print (url)
        if("../" in url):
            root_url = get_url_root(serch_url)
            if(root_url!=0):
                url = url.replace("..",root_url)
                print url
                download(url)
        else:
            download(input_url + url)

After editing


    for j in range(0,(len_url-1)):
        url = number_url[j]
        print (url)
        if("../" in url):
            root_url = get_url_root(serch_url)
            if(root_url!=0):
                url = url.replace("..",root_url)
                print url
                download(url)
        elif("http://" in url):
            download(url)
        elif("https://" in url):
            download(url)
        else:
            download(input_url + url)

All chords

get_image.py


# -*- coding: utf-8 -*- 

import urllib
import urllib2
import os.path
import sys
from HTMLParser import HTMLParser

def download(url):
    img = urllib.urlopen(url)
    localfile = open(os.path.basename(url), 'wb')
    localfile.write(img.read())
    img.close()
    localfile.close()

def get_url_root(url):
    if("http://" in url):
        url_delet_http = url.lstrip("http://")
        if("/" in url_delet_http):
            url_root = "http://" + url_delet_http[0:url_delet_http.find("/")]
            return url_root
    elif("https://" in url):
        url_delet_http = url.lstrip("https://")
        if("/" in url_delet_http):
            url_root = "http://" + url_delet_http[0:url_delet_http.find("/")]
            return url_root
    return 0

class imgParser(HTMLParser):

    def __init__(self):
        HTMLParser.__init__(self)

    def handle_starttag(self,tagname,attribute):
        if tagname.lower() == "img":
            for i in attribute:
                if i[0].lower() == "src":
                    img_url=i[1]
                    #Creating a file that collects the URLs of the acquired photos
                    f = open("collection_url.txt","a")
                    f.write("%s\t"%img_url)
                    f.close()

if __name__ == "__main__":

    print('Enter the URL of the site where you want to get the photo.')
    input_url = raw_input('>>>  ')
    serch_url = input_url
    htmldata = urllib2.urlopen(serch_url)

    print('Currently getting image files...')

    parser = imgParser()
    parser.feed(htmldata.read())

    parser.close()
    htmldata.close()

    #Read the generated file
    f = open("collection_url.txt","r")
    for row in f:
        row_url = row.split('\t')
        len_url = len(row_url)
    f.close()

    number_url = []

    for i in range(0,(len_url-1)):
        number_url.append(row_url[i])

    for j in range(0,(len_url-1)):
        url = number_url[j]
        print (url)
        if("../" in url):
            root_url = get_url_root(serch_url)
            if(root_url!=0):
                url = url.replace("..",root_url)
                print url
                download(url)
        elif("http://" in url):
            download(url)
        elif("https://" in url):
            download(url)
        else:
            download(input_url + url)

    print('The image download is complete.')

    #Delete file
    os.remove("collection_url.txt")

Recommended Posts

Batch download images from a specific URL with python Modified version
Bulk download images from specific URLs with python
Bulk download images from specific site URLs with python
Download images from URL list in Python
Installation procedure for Python and Ansible with a specific version
Write a batch script with Python3.5 ~
Call a command from Python (Windows version)
[Python] Download original images from Google Image Search
Create a decision tree from 0 with Python (1. Overview)
Read line by line from a file with Python
Extract data from a web page with Python
Create a batch of images and inflate with ImageDataGenerator
URL shortening with Python
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
Check version with python
[Python] Start a batch file from Python and pass variables.
A memo that reads data from dashDB with Python & Spark
Detect objects of a specific color and size with Python
Hash with python and escape from a certain minister's egosa
Automatically download images with scraping
Bordering images with python Part 1
Make a fortune with Python
Specify python version with virtualenv
Create a directory with python
With skype, notify with skype from python!
Download csv file with python
How to batch start a python program created with Jupyter notebook
Try to extract a character string from an image with Python3
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Read a file in Python with a relative path from the program
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Pass a list by reference from Python to C ++ with pybind11
[Python] Try to recognize characters from images with OpenCV and pyocr
Number recognition in images with Python
Execute Python script from batch file
[Python] What is a with statement?
Call a Python function from p5.js.
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Call C from Python with DragonFFI
Touch a Python object from Elixir
Download images from "Irasutoya" using Scrapy
Using Rstan from Python with PypeR
Let's make a GUI with python.
Install Python from source with Ansible
Create folders from '01' to '12' with python
Solve ABC166 A ~ D with Python
I can't download images with Google_images_download
Building a virtual environment with Python 3
python / Make a dict from a list.
Solve ABC168 A ~ C with Python
Extract text from images in Python
Post multiple Twitter images with python
Make a recommender system with python
Run Aprili from Python with Orange
Post images from Python to Tumblr
Animate multiple still images with Python
Generate a Pre-Signed URL with golang
Load gif images with Python + OpenCV
[Python] Generate a password with Slackbot