python 爬取图片 并保存至docx

#!/usr/bin/evn python
# -*- coding: utf-8 -*-

import os
import time

import requests
from docx import Document
from docx.shared import Inches


class MyObject(object):
    def __init__(self):
        self.doc = Document()

    def get_url(self, id):
        response = requests.get('http://***/img/pict-%d.png' % id)
        pic_dic = "%s.jpg" % str(id)
        with open(pic_dic, "wb")as f:
            f.write(response.content)
        self.doc.add_picture(pic_dic, width=Inches(6))  # 添加图, 设置宽度

        os.remove(pic_dic)

    def main(self):
        for id in range(1, 42):
            self.get_url(id)
        self.doc.save('XXX.docx')


if __name__ == '__main__':
    t0 = time.time()

    mo = MyObject()
    mo.main()

    print("耗时:", time.time() - t0)

不知道如何直接保存爬取的图片到docx,只能先保存图片,在保存到docx,再删除图片。
doc.add_picture(pic_dic)

安装: pip install python-docx

 

你可能感兴趣的:(Python,Python;数据分析,爬虫,python)