python-------利用多进程,多线程写一个下载器代码

利用多进程写一个下载器

from multiprocessing import Process
from urllib import request
def downloder(url,isPicture=1):
    file_name=url.split('/')[-1]
    response=request.urlopen(url)
    content=response.read()
    if isPicture==1:
        with open(file_name,'wb') as fq:
            fq.write(content)
    else:
        content=content.decode('utf-8')
        with open(file_name,'w',encoding='utf-8') as fq:
            fq.write(content)
if __name__ == '__main__':
    url_list=['https://p1.ssl.qhimg.com/t0151320b1d0fc50be8.png'
        ,'https://p0.ssl.qhimg.com/t01840823620d908ff2.png',
              'http://lianghui.people.com.cn/2019npc/n1/2019/0316/c425476-30978927.html'
    ]
    for url in url_list:
        p=Process(target=downloder,args=(url,))
        p.start()

利用多线程写一个下载器

from threading import Thread
from urllib import request
class Thresd_class(Thread):
    def __init__(self,url):
        Thread.__init__(self)
        self.url=url
    def run(self,isPicture=1):
        file_name=self.url.split('/')[-1]
        response=request.urlopen(self.url)
        content=response.read()
        if isPicture==1:
            with open(file_name,'wb') as fq:
                fq.write(content)
        else:
            content=content.decode('utf-8')
            with open(file_name,'w',encoding='utf-8') as fq:
                fq.write(content)
if __name__ == '__main__':
    url_list=['http://kf.qq.com/faq/170101iEzyqa170101vyE7Vv.html'
              ,'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2437668214,2147435278&fm=26&gp=0.jpg'
            ,'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3843445883,2974888073&fm=26&gp=0.jpg'
    ]
    for url in url_list:
        t=Thresd_class(url)
        t.start()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值