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多线程下载网页图片并保存至特定目录》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇.net读取Windows登录用户信息(downmoon)处理精度丢之-如何解决下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用Python获取图片的物理尺寸(KB)

如何获取图片的物理尺寸,而非(width, height)? #! -*- coding: utf-8 -*- import requests import io url = "https://i.loli.net/2019/11/23/Q7NAVgfWp8YnhSl.jpg" image = requests.get(url).content #ima...

python与c#的交互模块pythonnet

 今天总结一下python与c#的交互模块pythonnet,其实微软也是有相应的解释器的,就是Ironpython,可是毕竟Ironpython还有很多东西没有从python那边继承过来,所以有时候用起来并不是那么爽。但是好在强大的社区总会给我们惊喜,pythonnet就是其中一个让我惊喜的模块,它可以平滑的和C#进行交互。但是网上这方面的资料还是太少了...

2.3、Python迭代器、列表解析及生成器(0530)

1、动态语言 sys.getrefcount()      //查看对象的引用计数 增加对象的引用计数场景 对象创建时:以赋值的方式,创建变量名的同时就会创建变量 将对象添加进容器时:类似list.append() 当对象被当作参数传递给函数时 多重目标赋值时:s1 = s2 = s3 = 'abc' 为对象创建另外的变量名 减少引用计数 引用此对象...

吴裕雄 python 机器学习——数据预处理字典学习模型

from sklearn.decomposition import DictionaryLearning #数据预处理字典学习DictionaryLearning模型 def test_DictionaryLearning(): X=[[1,2,3,4,5], [6,7,8,9,10], [10,9,8,7,6,],...

python AES加密 ECB PKCS5

class AesEbc16:  # 按块的大小, 一块一块的加密, 明文和密文长度一样   def __init__(self):     self.key = b"123qweqqqwerqwer"  # 加密和解密用同一个秘钥, 长度为 每块的长度     self.mode = AES.MODE_ECB  # ECB加密模式, 也是默认的模式, 创...

python之线程与线程池

# 进程是资源分配的最小单位,线程是CPU调度的最小单位.每一个进程中至少有一个线程。 # 传统的不确切使用线程的程序称为只含有一个线程或单线程程序,而可以使用线程的程序被称为多线程程序,在程序中使用一个线程的方法 # 被称为多线程 # 线程的模块: # thread >> 实现线程的低级接口 # threading>>>...