# 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. 为什么23种设计模式里面没有MVC?

    作者:lorio链接:https://www.zhihu.com/question/27738109/answer/100241918来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  2. python语言特性总结

    一直想学习python,虽然编程写了不少,但有时仍不得要领.这篇blog主要是记录python的一些主要特性. 前言 python学习总结,包括python的一些基本语法,高级特性,函数式编程,面向对 ...

  3. 简单机器学习人脸识别工具face-recognition python小试,一行代码实现人脸识别

    摘要: 1行代码实现人脸识别,1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片.其中每个人一张图片,图片以人的名字命名.2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片.3 ...

  4. HDU 1880 简单Hash

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1880] 中文题面,题意很简单: 题解: 把每个 魔咒 和 对应的功能分别Hash,然后分别映射到ma ...

  5. input限制中文字数

    我们知道input控件有一个maxlength属性可以控制输入字符的长度,但是,它并不会识别是汉字还是其他符号,所以输入maxlength个汉字显然是不符合要求的. 为了实现对带有汉字的输入框长度控制 ...

  6. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  7. hdu 3340 Rain in ACStar 线段树区间等差数列更新

    Rain in ACStar Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 ...

  8. VK Cup 2015 - Round 1 E. Rooks and Rectangles 线段树 定点修改,区间最小值

    E. Rooks and Rectangles Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemse ...

  9. python画激活函数图像

    导入必要的库 import math import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl mpl.r ...

  10. undefined null 各种值比较(面试题)

    undefined和null与任何有意义的值比较返回的都是false,但是null与undefined之间互相比较返回的是true. console.log(null == false); //fal ...