MySQL中事先保存好爬取到的图片链接地址。

然后使用多线程把图片下载到本地。

# coding: utf-8
import MySQLdb
import requests
import os
import re
from threading import Thread
import datetime header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/63.0.3239.132 Safari/537.36'}
file_path = 'F:\\mlu2'
if not os.path.exists(file_path):
os.mkdir(file_path) class Spider(object):
def __init__(self, file_path, header):
self.file_path = file_path
self.header = header @staticmethod
def timer(func):
def time_count(*args):
start_time = datetime.datetime.now()
func(*args)
end_time = datetime.datetime.now()
day = (end_time - start_time).days
times = (end_time - start_time).seconds
hour = times / 3600
h = times % 3600
minute = h / 60
m = h % 60
second = m
print "爬取完成"
print "一共用时%s天%s时%s分%s秒" % (day, hour, minute, second)
return time_count def get_link(self):
conn = MySQLdb.connect(host='localhost',
port=3306,
user='root',
passwd='',
db='mlu',
charset='utf8')
cur = conn.cursor()
sql = 'select image from msg limit 100' # image为事先爬取存到MySQL的图片链接地址
cur.execute(sql)
img_link = cur.fetchall()
return img_link def download(self, link):
filename = re.findall(r'.*/(.+)', link)[0]
try:
pic = requests.get(link, headers=self.header)
if pic.status_code == 200:
with open(os.path.join(self.file_path)+os.sep+filename, 'wb') as fp:
fp.write(pic.content)
fp.close()
print "下载完成"
except Exception as e:
print e @timer
def run_main(self):
threads = []
links = self.get_link()
for link in links:
img = str(link[0])
t = Thread(target=self.download, args=[img])
t.start()
threads.append(t)
for t in threads:
t.join() spider = Spider(file_path, header)
spider.run_main()

Python使用requests模块下载图片的更多相关文章

  1. python使用requests模块下载文件并获取进度提示

    一.概述 使用python3写了一个获取某网站文件的小脚本,使用了requests模块的get方法得到内容,然后通过文件读写的方式保存到硬盘同时需要实现下载进度的显示 二.代码实现 安装模块 pip3 ...

  2. Python爬虫之使用Fiddler+Postman+Python的requests模块爬取各国国旗

    介绍   本篇博客将会介绍一个Python爬虫,用来爬取各个国家的国旗,主要的目标是为了展示如何在Python的requests模块中使用POST方法来爬取网页内容.   为了知道POST方法所需要传 ...

  3. python之poplib模块下载并解析邮件

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之poplib模块下载并解析邮件 #https://github.com/michaelliao ...

  4. 从0开始学爬虫11之使用requests库下载图片

    从0开始学爬虫11之使用requests库下载图片 # coding=utf-8 import requests def download_imgage(): ''' demo: 下载图片 ''' h ...

  5. Python之requests模块-hook

    requests提供了hook机制,让我们能够在请求得到响应之后去做一些自定义的操作,比如打印某些信息.修改响应内容等.具体用法见下面的例子: import requests # 钩子函数1 def ...

  6. Python之requests模块-cookie

    cookie并不陌生,与session一样,能够让http请求前后保持状态.与session不同之处,在于cookie数据仅保存于客户端.requests也提供了相应到方法去处理cookie. 在py ...

  7. Python之requests模块-session

    http协议本身是无状态的,为了让请求之间保持状态,有了session和cookie机制.requests也提供了相应的方法去操纵它们. requests中的session对象能够让我们跨http请求 ...

  8. Python之requests模块-request api

    requests所有功能都能通过"requests/api.py"中的方法访问.它们分别是: requests.request(method, url, **kwargs) req ...

  9. python基础-requests模块、异常处理、Django部署、内置函数、网络编程

     网络编程 urllib的request模块可以非常方便地抓取URL内容,也就是发送一个GET请求到指定的页面,然后返回HTTP的响应. 校验返回值,进行接口测试: 编码:把一个Python对象编码转 ...

随机推荐

  1. 6月29-7月5日成都uber优步司机第一/二/三组奖励政策明细

    成都优步司机第一/二/三组奖励更新了,在写下文之前,我先吐槽一下:靠优步uber发财致富已成往事. 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全 ...

  2. 成都Uber优步司机奖励政策(1月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. cocos2dx - ActionManager内存泄露

    ActionManager memory leak cocos2d-x3.7 都3.7了还有这样的bug,真是好难过,不过还是好开源的,谁都可以贡献一下 问题描述: 当创建一个node,并让它run一 ...

  4. 2019年猪年海报PSD模板-第六部分

    14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1WdlIiIdj1VVWxI4je0ebKw            

  5. ant-design学习准备_1

    在学习ant-desin过程中,发现很多知识都不清楚,从现在开始,每天将自己学习到的知识进行一个总结记录,前端大佬勿扰勿喷.先介绍几个基础概念和一些常用命令: 1.什么是脚手架 我们经常在各个博客论坛 ...

  6. 了解Python控制流语句——while 语句

    while 语句 Python 中 while 语句能够让你在条件为真的前提下重复执行某块语句. while 语句是 循环(Looping) 语句的一种.while 语句同样可以拥有 else 子句作 ...

  7. angular-列表进行添加、编辑等操作时此行变色。

    今天接触了一个功能,就是在一个列表中,当你新增或者对第N列进行编辑,删除等操作时这一列会变颜色.让用户对操作了哪行数据更认识更清晰,刷新之后这行的颜色就会消失.我觉得这个很有意思,记录一下.效果如下. ...

  8. lintcode373 奇偶分割数组

    奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 您在真实的面试中是否遇到过这个题? Yes 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 我的方法:设定两个数组,分别 ...

  9. [python]np.loadtxt报错

    np.loadtxt报错 通过pandas生成的cvs数据利用nump.loadtxt读取的时候 tmp = np.loadtxt('test.csv', dtype=np.str, delimite ...

  10. CSP201409-2:画图

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...