Python利用requests模块下载图片实例代码
作者:傲娇的草履虫 发布时间:2023-11-18 16:10:13
标签:python,requests,下载
本文主要介绍的是关于Python利用requests模块下载图片的相关,下面话不多说了,来一起看看详细的介绍吧
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='729814',
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()
来源:https://www.cnblogs.com/delav/p/9398825.html


猜你喜欢
- 本文实例讲述了Python中@property的理解和使用。分享给大家供大家参考,具体如下:重看狗书,看到对User表定义的时候有下面两行
- 人类学是关于人的研究;社会人类学(social anthropology)是研究人类社会的学科。社会人类学还可以理解成“文化翻译”(the
- 为什么我写ASP分页教程要提到AJAX呢,因为我们要多练习一下编程过程中,结构化的重要性. 再加上很多朋友对分页感到很高深,所以一直都不敢去
- 前言innodb_data_file_path用来指定innodb tablespace文件,如果我们不在My.cnf文件中指定innodb
- 摘要PIL.Image.open读入的是RGB顺序,而opencv中cv2.imread读入的是BGR通道顺序 。cv2.imread会显示
- 简单类型内置到 Python 编程语言中的简单数据类型包括: bool  
- 最近的一些疫情信息很让人揪心,为了方便大家掌握疫情信息,在空闲之余做了一个关于 nCoV 的疫情监控小助手。主要的功能是通过企业微信的 We
- 删除 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manage
- 首先写一个简单的drf接口from rest_framework.views import APIViewfrom rest_framewo
- PyQt5 QtChart-区域图QAreaSeries用于创建区域图数据,传入两个QLineSeries对象。核心代码:series0 =
- php简介当前网络技术发展日新月异,各种基于服务端创建 * 站的脚本语言更是层出不穷。其中PHP以其简单、易用、可移植性强等特点,在众多的动
- docker-compose.yal文件中: redis: image: redis container_name:
- 使用php就不一样了,php包含了zlib的链接库,可以直接使用其相关功能,下面是我写的压缩和结压缩swf文件的例子: //没有加入判断sw
- 函数介绍Socket对象方法:服务端:函数描述.bind()绑定地址关键字,AF_INET下以元组的形式表示地址。常用bind((host,
- pytorch自定义不可导激活函数今天自定义不可导函数的时候遇到了一个大坑。首先我需要自定义一个函数:sign_fimport torchf
- 一、Python的字典在项目的开发过程中,如果遇到有映射关系的内容可以考虑使用Python中的字典进行存储数据,字典中冒号前的数据称为【键】
- 之前呢,我一直对GUI不是很感兴趣,但是呢,最近由于某些特殊原因,导致不得不用tkinter,需要实现一个渐变色,但是当我翻阅文档的时候,却
- Python有许多吸引力,如效率,代码可读性和速度,使其成为数据科学爱好者的首选编程语言。Python通常是希望升级其应用程序功能的数据科学
- 1、出现错误train_df = pd.read_csv( 'C:\Users\lenovo\Desktop\train.csv
- 一、前情提要相信来看这篇深造爬虫文章的同学,大部分已经对爬虫有不错的了解了,也在之前已经写过不少爬虫了,但我猜爬取的数据量都较小,因此没有过