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. 长沙优步Uber奖励政策(7.27~8.2)

    奖励前提 *必须满足当周评分4.7星及以上,且当周接单率70%及以上,才有资格获得奖励 *当周上线时间不低于7小时 *刷单和红线行为立即封号并取消当周全部奖励! *机场高速费用不参与翻倍奖励 *早高峰 ...

  2. 苏州Uber人民优步奖励政策

    人民优步(People's Uber)资费标准 起步价(Base Fare):¥0.00 每公里(Per KM):¥1.65 每分钟(Per Min):¥0.30 最低价(Min Fare):¥9.0 ...

  3. 1030: [JSOI2007]文本生成器

    1030: [JSOI2007]文本生成器 https://www.lydsy.com/JudgeOnline/problem.php?id=1030 分析: AC自动机+dp. 正难则反,求满足的, ...

  4. mysql主从集群搭建;(集群复制数据)

    1.搭建mysql 5.7环境chown mysql:mysql -R /data/groupadd mysqluseradd -g mysql mysql yum install numactlrp ...

  5. spring源码-开篇

    一.写博客也有一段时间了,感觉东西越来越多了,但是自己掌握的东西越来越少了,很多时候自己也在想.学那么多东西,到头来知道的东西越来越少了.是不是很奇怪,其实一点都不奇怪. 我最近发现了一个很大的问题, ...

  6. spark history server

    参考:http://blog.csdn.net/lsshlsw/article/details/44786575 为什么需要historyServer? 在运行Spark Application的时候 ...

  7. 在Win10中通过命令行打开UWP应用

    近期由于需要在WinX菜单中添加几个UWP应用,但发现很难找到相应的命令行,Universal Apps 的快捷方式属性里也没有. 于是到网上搜了很久才找到一个E文的页面,试了一下确实可行,分享给大家 ...

  8. react-native windows系统 红屏报assets缺失 500错误

    指定版本,react-native是facebook用mac系统开发的,windows系统兼容较差,新版本更是问题很多, 相对老版本更加稳定 react-native init demo --vers ...

  9. PS 抠图和添加背景图

    1.打开需要抠的图--然后使用套索类工具,魔棒类工具,钢笔类工具均可选择需要扣的图片范围任何在Delete(如果抠反了可以进行反选Ctrl +shift+I) 2.然后把任一一张背景图直接拖到PS里面 ...

  10. photoshop cc 2018安装破解教程(破解补丁,亲测,绝对可用)

    破解步骤说明:下载地址百度网盘,https://pan.baidu.com/s/1cWtpUesl2fms3tFwEC0MiQ 1.右键解压Adobe Photoshop CC 2018 64位这个文 ...