加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python3爬取糗百硬菜图片 转自www.ustchacker.com

发布时间:2020-12-17 17:30:47 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import urllib.requestimport reimport threading images=[]def touchImages(): url='http://www.qiushibaike.com/imgrank' req=urllib.request.Reque

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import urllib.request
import re
import threading 


images=[]



def touchImages():
    url='http://www.qiushibaike.com/imgrank'
    req=urllib.request.Request(url,headers={'User-Agent':'Mozilla/5.0'})
    res=urllib.request.urlopen(req)
    html=res.read().decode('utf8')
    rule=re.compile('<img src="(.?)" alt="(.?)" />')
    return rule.findall(html)



class downImg(threading.Thread):
    def init(self):
        threading.Thread.init(self)
    def run(self):
        global images
        while images:
            image=images.pop()
            link=image[0]
            imgType=link.split('.')[-1]
            name=image[1]+'.'+imgType
            with open(name,'wb') as f:
                f.write(urllib.request.urlopen(link).read())



def multiDown(n):
    global images
    images+=touchImages()
    print('一共有'+str(len(images))+'张糗百图片')
    for i in range(n):
        a=downImg()
        a.start()



if name=='main':
    multiDown(10) #开10个线程去下载

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读