٤Ƴ٤Ĥ
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
# -*- 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_https = 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]
                    # 取得した写真のURLを集めたファイルの作成
                    f = open("collection_url.txt","a")
                    f.write("%s\t"%img_url)
                    f.close()
 
if __name__ == "__main__":
 
    print('写真を取得したいサイトのURLを入力してください。')
    input_url = raw_input('>>>  ')
    serch_url = input_url
    htmldata = urllib2.urlopen(serch_url)
 
    print('現在画像ファイルを取得中です...')
 
    parser = imgParser()
    parser.feed(htmldata.read())
 
    parser.close()
    htmldata.close()
 
    # 生成したファイルの読み込み
    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]
        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(url)
 
    print('画像のダウンロードが終了しました。')
 
    # ファイルの削除
    os.remove("collection_url.txt")
 

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-06-03 (水) 23:42:24 (1416d)