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


import re
import urllib.request

def getHtml(url):
    page=urllib.request.urlopen(url)
    html=page.read()
    return html
    
def getImg(html):
    reg = r'src="([.*\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")

优质内容筛选与推荐>>
1、由数据库数据生成XML的方法
2、Libevent 学习笔记 (1)——Libevent 2.0安装与简单演示样例
3、Spring Boot2.0之 yml的使用
4、AspNetPager分页控件--使用方法 2
5、查看linux服务器CPU相关


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号