CourseParser.py 文件源码

python
阅读 45 收藏 0 点赞 0 评论 0

项目:OUXMLConverter 作者: chaotic-kingdoms 项目源码 文件源码
def download_images(self, content):
        element = ElementTree.fromstring(content)

        images_dir = os.path.join(self.output_path, 'temp', 'images')
        if not os.path.exists(images_dir):
            os.makedirs(images_dir)

        for i, session in enumerate(element.iter('item'), start=1):
            try:
                print '    * Session ' + str(i) + ":",
                description = ElementTree.tostring(session.find('description'), 'utf8', 'xml')
                # print description.text
                images_list = re.findall('http[s]?://[^\s]*\.(?:jpg|JPG|png|PNG|jpeg|JPEG)', description)
                if len(images_list) == 0:
                    print 'No images to download.'
                    continue
                else:
                    print '\n      > Getting images from RSS file content... '

                    for idx, image_url in enumerate(images_list):
                        progress = str(idx * 100 / len(images_list)) + '%'
                        print '\r      > Downloading images (' + progress + ')',
                        sys.stdout.flush()

                        err, image = URLUtils.get(image_url)
                        if not err:
                            filename = image_url.split("/")[-1].replace(".small", "")
                            f = open(os.path.join(images_dir, filename), "wb+")
                            f.write(image)
                            f.close()

                    print '\r      > Downloading images (100%). Done.'

            except AttributeError:
                return
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号