20170912多线程Python爬取图片
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爬取图片的更多相关文章
- Python多线程Threading爬取图片,保存本地,openpyxl批量插入图片到Excel表中
之前用过openpyxl库保存数据到Excel文件写入不了,换用xlsxwriter 批量插入图片到Excel表中 1 import os 2 import requests 3 import re ...
- python 爬取图片
使用python的requests库爬取网页时,获取文本一般使用text方法,如果要获取图片并保存要用content 举个栗子,爬煎蛋网的图: #!/usr/bin/env python #-*- c ...
- Python爬去图片实例,python 爬取图片
# coding:utf-8 import requests import re import time proxies = { "http": "http://124. ...
- python爬取图片
1.导入需要的模块requests,BeautifulSoup,os(用于文件读写). 2.创建一个类,并初始化. 1 2 3 4 5 6 7 8 class BeautifulPicture: ...
- python多线程爬取图片实例
今天试着把前面那个爬取图片的爬虫改成了多线程爬取,虽然最后可以爬取存储图片了,但仍存在一些问题.网址还是那个网址https://www.quanjing.com/category/1286521/1. ...
- [python爬虫] 爬取图片无法打开或已损坏的简单探讨
本文主要针对python使用urlretrieve或urlopen下载百度.搜狗.googto(谷歌镜像)等图片时,出现"无法打开图片或已损坏"的问题,作者对它进行简单的探讨.同时 ...
- python如何使用request爬取图片
下面是代码的简单实现,变量名和方法都是跑起来就行,没有整理,有需要的可以自己整理下: image2local: import requests import time from lxml import ...
- 孤荷凌寒自学python第八十二天学习爬取图片2
孤荷凌寒自学python第八十二天学习爬取图片2 (完整学习过程屏幕记录视频地址在文末) 今天在昨天基本尝试成功的基础上,继续完善了文字和图片的同时爬取并存放在word文档中. 一.我准备爬取一个有文 ...
- 孤荷凌寒自学python第八十一天学习爬取图片1
孤荷凌寒自学python第八十一天学习爬取图片1 (完整学习过程屏幕记录视频地址在文末) 通过前面十天的学习,我已经基本了解了通过requests模块来与网站服务器进行交互的方法,也知道了Beauti ...
随机推荐
- python --- 10 *args **kwargs 命名空间 作用域 函数的嵌套
函数进阶 1. "*" 和 "**" ① 在形参位置时 都是聚合的作用 *args 位置参数→元组 **kwargs 关键字参数→字典 混 ...
- Django框架 (一) 虚拟环境配置及简单使用
虚拟环境 什么是虚拟环境 对真实的python解释器的一个拷贝版本 是事实有效的,可以独立存在运行解释python代码 可以在计算机上拷贝多个虚拟环境 为什么要使用虚拟环境 保证真实环境的纯净性 框架 ...
- HDU 4638 Group(莫队)题解
题意:n个数,每个数有一个值,每次询问一个区间,问你这个区间能分成连续的几段(比如7 1 2 8 就是两端 1 2 和 7 8) 思路:莫队.因为L.R移动顺序wa了20发...问了一下别人,都是先扩 ...
- R read.tabe line 5 did not have 2 elements
R read.tabe line 5 did not have 2 elements Reason: there are special characters such as # in file o ...
- BZOJ 4159 [Neerc2009]Business Center
思路 简单的模拟,答案就是\(min\{(\lfloor\frac{d\times n}{u+d}\rfloor+1)\times(u+d)-d\times n\}\) 代码 #include < ...
- 解决: docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
直接获取 rpm文件 wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.1 ...
- A Simple Note on "P4FPGA: A Rapid Prototyping Framework for P4"
论文:P4FPGA: A Rapid Prototyping Framework for P4 Github:https://github.com/p4fpga Reference: Han Wang ...
- awk详解2
7.控制语句 if(condition) {statments} 单分支语句 if(condition) {statments} else {statements}组合语句 while(condito ...
- 【译】第14节---数据注解-MaxLength/MinLength
原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...
- BZOJ 4808: 马(二分图最大点独立集)
http://www.lydsy.com/JudgeOnline/problem.php?id=4808 题意: 思路: 这图中的两个马只能选一个,二选一,很像二分图吧,对能互吃的两个棋子连线,在所选 ...