zoukankan      html  css  js  c++  java
  • python批量下载图片3

    import urllib.request
    import os
    
    def url_open(url):
    
        req = urllib.request.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36')
        response = urllib.request.urlopen(url)
        html = response.read()
        return html
    
    def get_page(url):
        html =  url_open(url).decode('utf-8')
        a = html.find('current-comment-page') + 23
        b = html.find(']',a)
        return html[a:b]
    
    
    def find_imgs(url):
        html = url_open(url).decode('utf-8')
        img_addrs = []
        a = html.find('img src=')
        while a!=-1:
            b = html.find('.jpg',a,a+255)
            if b != -1:
                img_addrs.append(html[a+9:b+4])
            else:
                b = a + 9
            a = html.find('img src=',b)
        return img_addrs
    
    def save_imgs(folder,img_addrs):
         for each in img_addrs:
              filename = each.split('/')[-1]
              urllib.request.urlretrieve(each,filename,None)
    
    def download_mm(folder='OOXX',pages=10):
        #os.mkdir(folder)
        os.chdir(folder)
        url = "http://konachan.com/post?tags=rating%3Asafe"
        page_num = int(2)
        for i in range(pages):
            page_num += i
            print(i)
            page_url = 'http://konachan.com/post?page=' + str(page_num) + '&tags=rating%3Asafe'
            print(page_url)
            img_addrs = find_imgs(page_url)
            save_imgs(folder,img_addrs)
    if __name__=='__main__':
        download_mm()

    import urllib.request
    import os
    
    def url_open(url):
        
        req = urllib.request.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36')
        response = urllib.request.urlopen(url)
        html = response.read()
        return html
    
    def get_page(url):
        html =  url_open(url).decode('utf-8')
        a = html.find('current-comment-page') + 23
        b = html.find(']',a)
        return html[a:b]
    
    
    def find_imgs(url):
        html = url_open(url).decode('utf-8')
        img_addrs = []
        a = html.find('img src=')
        while a!=-1:
            b = html.find('.jpg',a,a+255)
            if b != -1:
                img_addrs.append(html[a+9:b+4])
            else:
                b = a + 9
            a = html.find('img src=',b)
        return img_addrs
    
    def save_imgs(folder,img_addrs):
         for each in img_addrs:
              filename = each.split('/')[-1]
              with open(filename,'wb')as f:
                    img = url_open(each)
                    f.write(img)
              f.close() #每次存储之后都要close()否则存储的只是一个图
    def download_mm(folder='OOXX',pages=10): os.mkdir(folder) os.chdir(folder) url = "http://konachan.com/post?tags=rating%3Asafe" page_num = int(2) for i in range(pages): page_num += i print(i) page_url = 'http://konachan.com/post?page=' + str(page_num) + '&tags=rating%3Asafe' print(page_url) img_addrs = find_imgs(page_url) save_imgs(folder,img_addrs) if __name__=='__main__': download_mm()
  • 相关阅读:
    阿里双11,如何实现每秒几十万的高并发写入
    记住:永远不要在 MySQL 中使用 UTF-8
    史上最烂的项目:苦撑 12 年,600 多万行代码
    除了不要 SELECT * ,使用数据库还应知道的11个技巧!
    厉害了,为了干掉 HTTP ,Spring团队又开源 nohttp 项目!
    请停止学习框架
    基于 MySQL 主从模式搭建上万并发的系统架构
    JS获取节点
    JS函数
    JS
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4906128.html
Copyright © 2011-2022 走看看