【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 ...
随机推荐
- Python操作Mongo数据库
连接数据库 import pymongo # 连接到数据库,以下两种方式均可 client = pymongo.MongoClient(host='localhost', port=27017) cl ...
- 【BZOJ 3534】 3534: [Sdoi2014]重建 (Matrix-Tree Theorem)
3534: [Sdoi2014]重建 Time Limit: 10 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 709 Solved: 32 ...
- CodeForces 1065E. Side Transmutations 计数
昨天不该早点走的.... 首先操作限制实际上是一个回文限制 每个$b[i] - b[i - 1]$互不干扰,不妨设这个串关于中心点对称的这么一对区间的串分别为$(S_1, S_2)$ 题目的限制相当与 ...
- loj2576 「TJOI2018」str
link 题意: 给一个模板串s和n个模式串,每个模式串有$a_i$种可取的串.现在要将n个模式串每个任取一种它可取的串,连接起来,记为串t,那么这种连接方式对答案的贡献为t在s中出现的次数.问所有连 ...
- JDK源码(1.7) -- java.util.Queue<E>
java.util.Queue<E> 源码分析(JDK1.7) -------------------------------------------------------------- ...
- Codeforces Round #262 (Div. 2) E. Roland and Rose 暴力
E. Roland and Rose Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- bzoj 2428: [HAOI2006]均分数据 随机化
2428: [HAOI2006]均分数据 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...
- 2015编程之美 初赛第一场C题 质数相关 二分图的最大匹配
质数相关 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/msbop2015round2a/prob ...
- Pandas中Series和DataFrame的索引
在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引.比如list进行索引的时候使用的是下标,而dict索引的时候使用的是关键字. ...
- python string和dict转换
字典(dict)转为字符串(string) 我们可以比较容易的将字典(dict)类型转为字符串(string)类型. 通过遍历dict中的所有元素就可以实现字典到字符串的转换: for key, va ...