python多线程下载网页图片并保存至特定目录
#!python3
#multidownloadXkcd.py - Download XKCD comics using multiple threads. import requests
import bs4
import os
import threading # os.mkdir('xkcd', exist_ok=True) # store comics in ./xkcd
if os.path.exists('xkcd'):
print("xkcd is existed!")
else:
os.mkdir('xkcd') def downloadXkcd(startComic, endComic):
for urlNumber in range(startComic, endComic):
#Download the page
print("Downloading page http://xkcd.com/%s..." % urlNumber)
res = requests.get('http://xkcd.com/%s' % urlNumber)
res.raise_for_status() print(res.text)
soup = bs4.BeautifulSoup(res.text) #Find the URL of the comic image.
comicElem = soup.select('#comic img')
if comicElem == []:
print('Could not find comic images.')
else:
comicUrl = comicElem[0].get('src')
# #Download the image.
# print('Downloading image %s...' % (comicUrl))
# res = requests.get(comicUrl)
# res.raise_for_status()
#
# # Save the image to ./xkcd
# imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')
# for chunk in res.iter_content(100000):
# imageFile.write(chunk)
# imageFile.close() downloadThread = threading.Thread(target=downloadXkcd(555, 557))
downloadThread.start() # # TODO: Create and start the thread objects
# downloadThreads = [] # a list of all the Thread objects
# for i in range(500, 600, 10):
# downloadThread = threading.Thread(target=downloadXkcd, args=(i, i+9))
# downloadThreads.append(downloadThread)
# downloadThread.start()
#
# # TODO: Wait for all threads to end
# for downloadThread in downloadThreads:
# downloadThread.join()
# print("Done.")
python多线程下载网页图片并保存至特定目录的更多相关文章
- php中ob_get_contents、curl_multi_init、curl_init多线程下载远程图片并保存记录
php中三种方式测试图片下载效率 原文共24张不同图,每张大小在500K以上 使用时注意调整传入数组格式以及需要下载时保存地址的路径格式等 这三种方式无需额外安装扩展,方便快捷易操作[虽然效率看结果没 ...
- python:将网页图片保存到本地
1.requests库介绍 在python中,有一个非常好用的网络请求库requests,使用它可以发起网络请求,并获取网页返回的内容.同时,也可以进行网页图片下载 requests是使用Apache ...
- 小白学Python(7)——利用Requests下载网页图片、视频
安装 Requests 如果安装了Requests就已经可用了,否则要安装 Requests,只要在你的CMD中运行这个简单命令即可: pip install requests requests使用 ...
- Python爬虫下载美女图片(不同网站不同方法)
声明:以下代码,Python版本3.6完美运行 一.思路介绍 不同的图片网站设有不同的反爬虫机制,根据具体网站采取对应的方法 1. 浏览器浏览分析地址变化规律 2. Python测试类获取网页内容,从 ...
- Python爬虫之网页图片抓取
一.引入 这段时间一直在学习Python的东西,以前就听说Python爬虫多厉害,正好现在学到这里,跟着小甲鱼的Python视频写了一个爬虫程序,能实现简单的网页图片下载. 二.代码 __author ...
- 用 Python 批量下载百度图片
为了做一个图像分类的小项目,需要制作自己的数据集.要想制作数据集,就得从网上下载大量的图片,再统一处理. 这时,一张张的保存下载,就显得很繁琐.那么,有没有一种方法可以把搜索到的图片直接下载到本地 ...
- python爬取网页图片(二)
从一个网页爬取图片已经解决,现在想要把这个用户发的图片全部爬取. 首先:先找到这个用户的发帖页面: http://www.acfun.cn/u/1094623.aspx#page=1 然后从这个页面中 ...
- python脚本工具-1 制作爬虫下载网页图片
参考:http://www.cnblogs.com/fnng/p/3576154.html 本文参考虫师的博客“python实现简单爬虫功能”,整理分析后抓取其他站点的图片并下载保存在本地. 抓取图片 ...
- Python多线程Threading爬取图片,保存本地,openpyxl批量插入图片到Excel表中
之前用过openpyxl库保存数据到Excel文件写入不了,换用xlsxwriter 批量插入图片到Excel表中 1 import os 2 import requests 3 import re ...
随机推荐
- Java happen-before
下面是happens-before原则规则: 程序次序规则:一个线程内,按照代码顺序,书写在前面的操作先行发生于书写在后面的操作: 锁定规则:一个unLock操作先行发生于后面对同一个锁额lock操作 ...
- mongodb突然出现一些特别奇葩的事
mongo突然出现一些奇葩的事,如数据都还在,但某个命令敲下去了.啥东西都没有返回给我们. 往往这个时候特别的郁闷,找不出问题所在. 不用太担心,看看版本,多半是mongo的版本太老了,有些命令已经过 ...
- Python 图示集绵
http://nbviewer.jupyter.org/github/pyecharts/pyecharts-users-cases/blob/master/notebook-users-cases/ ...
- Realtime Rendering 1.1
[Realtime Rendering 1.1] 1.A linear transform is one that preserves vector addition and scalar multi ...
- 【OpenGL】glsl、glew、glfw
glsl: OpenGL着色语言(OpenGL Shading Language)是用来在OpenGL中着色编程的语言,也即开发人员写的短小的自定义程序,他们是在图形卡的GPU (Graphic Pr ...
- 10大H5前端框架(转)
10大H5前端框架 作为一名做为在前端死缠烂打6年并且懒到不行的攻城士,这几年我还是阅过很多同门从知名到很知名的各种前端框架,本来想拿15-20个框架来分享一下,但在跟几个前辈讨教写文章的技巧时果断被 ...
- C/s程序过时了吗?
目前的程序从原来的形态演变成了 C/s,B/s,和手机端. 其实应该各有自己的客户群,及定位. 比如C/s为单机版的可以完成个性化突出的复杂客户端应用,企业级别的应用. B/s的特点安装简单,功能制作 ...
- 设置input标签的placeholder的样式
设置input样式代码: input::-webkit-input-placeholder{ /*WebKit browsers*/ color: red; } input::-moz-input-p ...
- awk——getline
A.getline从整体上来说,应这么理解它的用法: 当其左右无重定向符 | 或 < 时,getline作用于当前文件,读入当前文件的第一行给其后跟的变量var 或$0(无变量):应该注意到,由 ...
- 二进制中1的个数(python)
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. # -*- coding:utf-8 -*- class Solution: def NumberOf1(self, n): ...