import threading               #导入线程
from urllib import request #导入网页请求模块
import re #导入正则表达式模块
import os # 引入模块
from openpyxl import Workbook
from openpyxl import load_workbook
class customThread(threading.Thread):
def __init__(self,imgurl,imgpath):
threading.Thread.__init__(self)
self.imgurl=imgurl
self.imgpath=imgpath
def run(self):
#print('downloading : ',self.imgpath)
downloadimg(self.imgurl,self.imgpath)
def downloadimg(imgurl,imgpath):
try: #实践中发现会出现网页访问失败返回404的情况
response=request.urlopen(imgurl) #访问图片地址
imgcontents=response.read() #获取图片内容
except:
print(imgpath +'下载出错')
else:
f=open(imgpath,'wb') #打开文件
f.write(imgcontents) #写入内容
f.close #关闭文件
print('保存成功>>>>'+ imgpath ) def getimageurl(weburl,folder,imgname):
response=request.urlopen(weburl)#打开网页,获取响应文本
page=response.read() #读取网页源码
js= page.decode('utf-8') #转码
print(js)
pat=re.compile(r'(?<="//)www.dhresource.com/.*?\.jpg') #编译匹配模式
match=re.findall(pat,js) #匹配网页源码
if match: #若匹配则输出
#print ('匹配成功')
n=0
for each_match in match:
n+=1
imgurl='http://'+each_match
imgpath=folder +"\\"+ imgname+"_" +str(n)+'.jpg'
customThread(imgurl,imgpath).start() #调用下载图片函数
def mkdir(path):
# 去除首位空格
path=path.strip()
# 去除尾部 \ 符号
path=path.rstrip("\\")
# 判断路径是否存在
isExists=os.path.exists(path)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
print(path+' 创建成功')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print(path+' 目录已存在')
return False if __name__ == "__main__":
print('!!!!!!开始运行!!!!!!')
wb = load_workbook('URL.xlsx')
ws=wb.active
for i in range(2,51):
#print(ws.cell(row=i,column=1).value)
if ws.cell(row=i,column=1).value!=None:
imgname=str(ws.cell(row=i,column=1).value)
folder=os.getcwd() +"\\"+imgname
print(mkdir(folder))
weburl=ws.cell(row=i,column=2).value
getimageurl(weburl,folder,imgname) print('!!!!!!运行结束!!!!!!')

  

20170912多线程Python爬取图片的更多相关文章

  1. Python多线程Threading爬取图片,保存本地,openpyxl批量插入图片到Excel表中

    之前用过openpyxl库保存数据到Excel文件写入不了,换用xlsxwriter 批量插入图片到Excel表中 1 import os 2 import requests 3 import re ...

  2. python 爬取图片

    使用python的requests库爬取网页时,获取文本一般使用text方法,如果要获取图片并保存要用content 举个栗子,爬煎蛋网的图: #!/usr/bin/env python #-*- c ...

  3. Python爬去图片实例,python 爬取图片

    # coding:utf-8 import requests import re import time proxies = { "http": "http://124. ...

  4. python爬取图片

    1.导入需要的模块requests,BeautifulSoup,os(用于文件读写). 2.创建一个类,并初始化.   1 2 3 4 5 6 7 8 class BeautifulPicture: ...

  5. python多线程爬取图片实例

    今天试着把前面那个爬取图片的爬虫改成了多线程爬取,虽然最后可以爬取存储图片了,但仍存在一些问题.网址还是那个网址https://www.quanjing.com/category/1286521/1. ...

  6. [python爬虫] 爬取图片无法打开或已损坏的简单探讨

    本文主要针对python使用urlretrieve或urlopen下载百度.搜狗.googto(谷歌镜像)等图片时,出现"无法打开图片或已损坏"的问题,作者对它进行简单的探讨.同时 ...

  7. python如何使用request爬取图片

    下面是代码的简单实现,变量名和方法都是跑起来就行,没有整理,有需要的可以自己整理下: image2local: import requests import time from lxml import ...

  8. 孤荷凌寒自学python第八十二天学习爬取图片2

    孤荷凌寒自学python第八十二天学习爬取图片2 (完整学习过程屏幕记录视频地址在文末) 今天在昨天基本尝试成功的基础上,继续完善了文字和图片的同时爬取并存放在word文档中. 一.我准备爬取一个有文 ...

  9. 孤荷凌寒自学python第八十一天学习爬取图片1

    孤荷凌寒自学python第八十一天学习爬取图片1 (完整学习过程屏幕记录视频地址在文末) 通过前面十天的学习,我已经基本了解了通过requests模块来与网站服务器进行交互的方法,也知道了Beauti ...

随机推荐

  1. CentOS7下Docker中构建可以自动发布到项目的Tomcat容器

    步骤 下载镜像 搜索相应的镜像文件:docker search 'tomcat' 如下 下载镜像:docker pull tomcat:7,如下图 PS:后面的数字代表tomcat的版本,可以自己选择 ...

  2. secureCRT的自动化脚本如何编写?

    以等待字符串eth0的出现,出现后或者20秒后脚本执行reboot命令的脚本为例,示例如下: #$language = "VBScript" #$interface = " ...

  3. vim改善生活的几个插件

    vim改善生活的几个插件 http://www.cnblogs.com/lovesaber/archive/2012/01/06/2315343.html

  4. X-Pack for the Elastic Stack [6.2] » Securing the Elastic Stack »Setting Up User Authentication

    https://www.elastic.co/guide/en/x-pack/current/setting-up-authentication.html Active Directory User ...

  5. 【问题解决】连接mysql 8错误:authentication plugin 'caching_sha2_password

    在刚安装好mysql8,使用native连接的时候报错 authentication plugin 'caching_sha2_password'... 首先确保服务已开启,然后通过cmd命令进入my ...

  6. 题解——HDU 1848 Fibonacci again and again

    一道组合游戏的题目 SG函数的板子题 预处理出SG函数的值然后回答询问即可 代码 #include <cstdio> #include <algorithm> #include ...

  7. Deep Learning framework --- MexNet 安装,测试,以及相关问题总结

    Deep Learning framework --- MexNet 安装,测试,以及相关问题总结  一.安装:   参考博文:http://www.open-open.com/lib/view/op ...

  8. $mount方法是用来挂载我们的Vue.extend扩展的

    html <body> <div id="app"> <diy></diy> </div> </body> ...

  9. 【面试问题】mybatis 与 Hibernate的不同

    Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己编写Sql语句.mybatis可以通过XML或注解方式灵活配置要运行的sql语句,并将java对象和s ...

  10. HDU 5583 Kingdom of Black and White(暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...