【pyhon】nvshens按目录图片批量下载爬虫1.00(多线程版)
# 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(多线程版)的更多相关文章
- Node.js mzitu图片批量下载爬虫1.00
又攻下一座山头. //====================================================== // mzitu图片批量下载爬虫1.00 // 2017年11月19 ...
- Node.js 4493图片批量下载爬虫1.00
这个爬虫依然需要iconv转码,想不到如今非utf8的网页还这么多.另外此网页找下一页的方式比较异常,又再次借助了正则表达式. 代码如下: //============================ ...
- Node.js monly图片批量下载爬虫1.00
此爬虫又用到了iconv转码,代码如下: //====================================================== // mmonly图片批量下载爬虫1.00 ...
- Node.js m03122图片批量下载爬虫1.00
//====================================================== // m03122图片批量下载爬虫1.00 // 2017年11月18日 //==== ...
- Node.js mm131图片批量下载爬虫1.00 iconv协助转码
//====================================================== // mm131图片批量下载爬虫1.00 // 2017年11月15日 //===== ...
- Node.js nvshens图片批量下载爬虫 1.00
//====================================================== // www.nvshens.com图片批量下载Node.js爬虫1.00 // 此程 ...
- Node.js mimimn图片批量下载爬虫 1.00
这个爬虫在Referer设置上和其它爬虫相比有特殊性.代码: //====================================================== // mimimn图片批 ...
- 【pyhon】nvshens图片批量下载爬虫1.01
# nvshens图片批量下载爬虫1.01 # 原先版本在遇到网络故障时回下载不全,这回更改了模式使得下载不成就重新下载,直到全部下载完毕 from bs4 import BeautifulSoup ...
- 【pyhon】nvshens图片批量下载爬虫
代码: # nvshens图片批量下载爬虫 from bs4 import BeautifulSoup import requests import time import urllib.reques ...
随机推荐
- Android升级ADT22后会报ClassNotFoundException的原因分析
http://blog.csdn.net/huzgd/article/details/8962702 1.ADT16下,只要add to path就是add to path并export:2.ADT2 ...
- python中if和elif的区别
多个if语句是每次单独判断 比如: 例子一: a = 5 if a < 6: #条件1 print(1) if a < 7: #条件2 print(2) else: print(3) 条件 ...
- CSS的flex布局(转载)
我们之前已经学过一些布局模型,比如说浮动,绝对定位等等,但是这些布局方式一是不够简洁,而是使用的范围确实是太窄了. flex模型拥有比较多的属性,来设置多样的布局方式,接下来我们就详细介绍各种属性对布 ...
- JAVA对数字证书的常用操作(转载)
一:需要包含的包 import java.security. * ; import java.io. * ; import java.util. * ; import java.security. * ...
- [转]Intent和IntentFilter详解
Intent Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描 ...
- mybatis源码分析(1)-----sqlSessionFactory创建
1. 首先了解一下mybatis,包含核心jar ,以及spring相关jar. <!-- Mybatis相关组件 --> <dependency> <groupId&g ...
- CC1101是一种低成本真正单片的UHF收发器
CC1101是一种低成本真正单片的UHF收发器,为低功耗无线应用而设计.电路主要设定为在315.433.868和915MHz的ISM(工业,科学和医学)和SRD(短距离设备)频率波段,也可以容易地设置 ...
- Integrate SharePoint 2013 with Team Foundation Server 2012
Now that SharePoint 2013 is out I want to make sure that I can integrate SharePoint 2013 with Team F ...
- 使用 pm2-web 监控 pm2 服务运行状态
pm2-web 是一款 pm2 服务状态监控程序,基于 web . 安装 $ npm install -g pm2-web 运行(默认是在8080端口) $ pm2-web 配置 pm2-web 将会 ...
- 每天进步一点点——关于SSD写入放大问题
转载请说明出处:http://blog.csdn.net/cywosp/article/details/29812433 1. 关于SSD的写入放大 之前在SSD(Solid State Dr ...