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

Posted huiziz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬虫:爬取图片并保存在某路径下相关的知识,希望对你有一定的参考价值。

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")

 

以上是关于爬虫:爬取图片并保存在某路径下的主要内容,如果未能解决你的问题,请参考以下文章

Python爬虫 自动爬取图片并保存

使用Scrapy爬虫框架简单爬取图片并保存本地(妹子图)

scrapy爬虫系列之三--爬取图片保存到本地及日志的基本用法

java爬虫爬取b站视频分享iframe代码并保存10000条数据到数据库

Python使用Scrapy爬虫框架全站爬取图片并保存本地(妹子图)

百度飞桨学习——七日打卡作业选手信息爬取