首页 > 其他 > 详细

爬虫:爬取图片并保存在某路径下

时间:2019-01-11 23:48:04      阅读:368      评论:0      收藏:0      [点我收藏+]
import re
import urllib.request

def getHtml(url):
    page=urllib.request.urlopen(url)
    html=page.read()
    return html
    
def getImg(html):
    reg = rsrc="([.*\S]*\.jpg)"
    imgre=re.compile(reg)
    imglist=re.findall(imgre,html)
    return imglist

    
html=getHtml("http://www.win4000.com/zt/gaoqing.html")
html=html.decode("utf-8")
#print (1,html[:500])

imgList=getImg(html)
#print (2,imgList[:500])
imgName=0
for imgPath in imgList:
    try:
        pic_content = (urllib.request.urlopen(imgPath)).read()
        if len(pic_content)>4000:
            f = open(E:\\workspace-python\\testtest\\+ str(imgName)+".jpg",wb)
            f.write(pic_content)
            print(imgPath)
            f.close()
    except Exception as e:
        print(imgPath+" error")
    imgName += 1
print ("All Done")

 

爬虫:爬取图片并保存在某路径下

原文:https://www.cnblogs.com/huiziz/p/10257725.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!