【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画一朵花
from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import Line ...
- 我的OI生涯 第五章
我的OI生涯 第五章 千古诗才,蓬莱文章建安骨 一身傲骨,青莲居士谪仙人 李白追月逆江河 包黑斩龙顺民心 豪气压群雄,能使力士脱靴,贵妃捧砚; 仙才媲众美,不让参军俊逸,开府清新 我辈此中惟饮酒 先生 ...
- 51nod1376 最长上升子序列的数量
机房的人问我树状数组怎么做这题...... 树状数组维护$len, num$表示$LIS$的长度和数量即可 复杂度$O(n \log n)$ 注:$O(n \log n)$二分+单调栈才是真神仙 具体 ...
- HDU 6118 度度熊的交易计划(费用流)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6118 [题目大意] 给出一张无向边权图,每个点最多可以生产b[i]商品,每件代价为a[i], 每个 ...
- bzoj 2809: [Apio2012]dispatching -- 可并堆
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Description 在一个忍者的帮派里,一些忍者们被选中派 ...
- 通过wifi上网,桥接模式下virtualBox虚拟机无法连上网的解决办法
https://jingyan.baidu.com/article/948f59242e601dd80ff5f929.html
- [转]Android:布局实例之模仿QQ登录界面
Android:布局实例之模仿QQ登录界面 预览图: 准备: 1.找到模仿对象 QQ登陆界面UI下载>>>>> 2.导入工程 3.查看布局结构和使用控件 其对应效果图分布 ...
- 51nod 1225 余数之和 数论
1225 余数之和 题目连接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225 Description F(n) ...
- CentOS6永久修改主机名称
1.修改network vi /etc/sysconfig/network 修改HOSTNAME值 2.修改hosts vi /etc/hosts 修改中间的那个localhost 3.使用hostn ...
- 几种OutOfMemoryError
JAVA虚拟机OutOfMemoryError主要包括以下四类:java.lang.OutOfMemoryError: Java heap spacejava.lang.OutOfMemoryErro ...