# nvshens按目录图片批量下载爬虫1.00(多线程版)
from bs4 import BeautifulSoup
import requests
import datetime
import urllib.request
import os
import threading

user_agent='Mozilla/4.0 (compatible;MEIE 5.5;windows NT)'
headers={'User-Agent':user_agent}

# 下载图片到本地
def downloadPics(pictures):
    while(len(pictures)>0):
        pic=pictures.pop()

        name=pic.split('/')[-1]
        folder=pic.split('/')[-2]

        # 判断目录是否存在,不存在则创建之
        if os.path.exists('./'+folder)==False:
            os.makedirs('./'+folder)

        try:
            rsp=urllib.request.urlopen(pic)
            img=rsp.read()
            with open('./'+folder+"/"+name,'wb') as f:
                f.write(img)
            print('图片'+pic+'下载完成')
        except Exception as e:
            print('图片'+pic+'下载异常,塞回重试')
            pictures.append(pic);

#下载线程类
class dldThread(threading.Thread):
    def __init__(self,name,url):
        threading.Thread.__init__(self,name=name)
        self.name=name
        self.url=url
        self.pictures=[]

    def run(self):
        while(self.url!="none"):
            print("线程"+self.name+"开始爬取页面"+self.url);

            try:
                rsp=requests.get(self.url,headers=headers)
                self.url="none"#用完之后置空,看下一页能否取到值
                soup= BeautifulSoup(rsp.text,'html.parser',from_encoding='utf-8')                

                for divs in soup.find_all(class_="gallery_wrapper"):
                    # 把找到的图片放到数组里去
                    for img in divs.find_all('img'):
                        print(img.get("src"))
                        self.pictures.append(img.get("src"))

                    #找下一页
                    for link in divs.find_all('a',class_='a1'):
                        if link.string=='下一页' and link.get("href").find('.html')!=-1:
                            self.url='https://www.nvshens.com'+link.get("href")

                if self.url!="none":
                    print("线程"+self.name+"前往下一页")
                    continue
                else:
                    print("线程"+self.name+'爬取结束,开始下载...')
                    downloadPics(self.pictures)
                    print("线程"+self.name+'下载图片结束.')
            except Exception as e:
                print("线程"+self.name+"发生异常。重新爬行")# 不管怎么出现的异常,就让它一直爬到底
                continue

# 循环下载图片
def main():
    for i in range(10000,20000):#范围自己调整
        url='https://www.nvshens.com/g/'+str(i)+'/'

        th=dldThread(name=str(i),url=url)
        th.start()

# Kickoff Start
main()

【pyhon】nvshens按目录图片批量下载爬虫1.00(多线程版)的更多相关文章

  1. Node.js mzitu图片批量下载爬虫1.00

    又攻下一座山头. //====================================================== // mzitu图片批量下载爬虫1.00 // 2017年11月19 ...

  2. Node.js 4493图片批量下载爬虫1.00

    这个爬虫依然需要iconv转码,想不到如今非utf8的网页还这么多.另外此网页找下一页的方式比较异常,又再次借助了正则表达式. 代码如下: //============================ ...

  3. Node.js monly图片批量下载爬虫1.00

    此爬虫又用到了iconv转码,代码如下: //====================================================== // mmonly图片批量下载爬虫1.00 ...

  4. Node.js m03122图片批量下载爬虫1.00

    //====================================================== // m03122图片批量下载爬虫1.00 // 2017年11月18日 //==== ...

  5. Node.js mm131图片批量下载爬虫1.00 iconv协助转码

    //====================================================== // mm131图片批量下载爬虫1.00 // 2017年11月15日 //===== ...

  6. Node.js nvshens图片批量下载爬虫 1.00

    //====================================================== // www.nvshens.com图片批量下载Node.js爬虫1.00 // 此程 ...

  7. Node.js mimimn图片批量下载爬虫 1.00

    这个爬虫在Referer设置上和其它爬虫相比有特殊性.代码: //====================================================== // mimimn图片批 ...

  8. 【pyhon】nvshens图片批量下载爬虫1.01

    # nvshens图片批量下载爬虫1.01 # 原先版本在遇到网络故障时回下载不全,这回更改了模式使得下载不成就重新下载,直到全部下载完毕 from bs4 import BeautifulSoup ...

  9. 【pyhon】nvshens图片批量下载爬虫

    代码: # nvshens图片批量下载爬虫 from bs4 import BeautifulSoup import requests import time import urllib.reques ...

随机推荐

  1. RxSwift 系列(七)

    前言 本篇文章将要学习RxSwift中连接操作符.Connectable Observable在订阅时不发射事件消息,而是仅当调用它们的connect()方法时才发射消息,这样就可以等待所有我们想要的 ...

  2. 矩阵&行列式

    # 代数 排列 对换,对于一个排列操作,对于一个偶排列一次对换之后变为奇排列 反之变为偶排列 行列式 N阶行列式室友N^2个数aij(i,j = 1,2,3,...n) 行列式的数=\(\sum_ { ...

  3. codevs 1392 合并傻子

    1392 合并傻子 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description 在一个园形操场的四周站着N个傻子,现要将傻子有 ...

  4. bzoj1116 [POI2008]CLO 边双联通分量

    只需对每个联通块的$dfs$树检查有没有返租边即可 复杂度$O(n + m)$ #include <cstdio> #include <cstring> using names ...

  5. bzoj 3165

    题意: 给出平面上一些线段,在线询问与x=x0相交的线段中,交点y最大的线段的标号,支持添加线段. 大概思路: 用线段树维护,线段树每个线段记录贯穿(左右端点在该区间外或上)的原线段中能覆盖其它贯穿该 ...

  6. PAT甲级1076. Forwards on Weibo

    PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...

  7. javascritp 字符串截取

    1.substring 方法 定义和用法 substring 方法用于提取字符串中介于两个指定下标之间的字符. 语法 ? stringObject.substring(start,stop) 参数   ...

  8. Redis-Linux安装

    简介 redis是一个开源项目,一种基于hash存储于内存的nosql数据库.和Memcached类似,但很大程度补偿了memcached的不足,它支持存储的value类型相对更多,包括string. ...

  9. JLink Support for the Nuvoton NUC1xx devices

    http://forum.segger.com/index.php?page=Thread&threadID=1441 Friday, April 19th 2013, 7:25pm Hi M ...

  10. cherokee +php fastcgi 出现 No input file specified 故障一例

    在arch上编译cherokee 时用的--with-wwwroot=/srv/http.在建立虚拟服务器时,只要虚拟服务器的根目录位于/srv/http下,php页面都能正确运行.但只要将拟服务器的 ...