前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 批量下载图片示例

Python 批量下载图片示例

作者头像
py3study
发布2020-01-09 15:04:15
7770
发布2020-01-09 15:04:15
举报
文章被收录于专栏:python3

    使用Python find函数和urllib下载图片。

A:

代码语言:javascript
复制
#!/usr/bin/env python
import time
import urllib
i = 0
url = ['']*10
name = ['']*10
con = urllib.urlopen('http://www.ithome.com/html/bizhi/164396.htm').read()
src = con.find(r'/newsuploadfiles')
end = con.find(r'.jpg',src)
name[0] = con[src +24 :end +1]
while src != -1 and end != -1 and i < 10:
    url[0] = con[src -21 :end +4]
    src = con.find(r'/newsuploadfiles',end)
    end = con.find(r'.jpg',src)
    content = urllib.urlopen(url[0]).read()
    open(r'img/'+ name[0]+'jpg','w+').write(content)
    name[0] = con[src +24 :end +1]
    print url[0]
    time.sleep(3)
    i = i + 1
else:
    print "Download Over!"

B:

代码语言:javascript
复制
#!/usr/bin/env python
import re
import urllib
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html
def getImg(html):
    reg = r'data-original="(.+?\.jpg)" />'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    i = 0
    for imgurl in imglist:
        name = imgurl[45:]
        print name
        content = urllib.urlopen(imgurl).read()
        open(r'tmp/' + name,'w+').write(content)
        i = i + 1
html = getHtml("http://www.ithome.com/html/bizhi/164396.htm")
print getImg(html)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档