飞道的博客

python 爬取图片 并保存至docx

453人阅读  评论(0)

  
  1. #!/usr/bin/evn python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import time
  5. import requests
  6. from docx import Document
  7. from docx.shared import Inches
  8. class MyObject(object):
  9. def __init__(self):
  10. self.doc = Document()
  11. def get_url(self, id):
  12. response = requests.get( 'http://***/img/pict-%d.png' % id)
  13. pic_dic = "%s.jpg" % str(id)
  14. with open(pic_dic, "wb") as f:
  15. f.write(response.content)
  16. self.doc.add_picture(pic_dic, width=Inches( 6)) # 添加图, 设置宽度
  17. os.remove(pic_dic)
  18. def main(self):
  19. for id in range( 1, 42):
  20. self.get_url(id)
  21. self.doc.save( 'XXX.docx')
  22. if __name__ == '__main__':
  23. t0 = time.time()
  24. mo = MyObject()
  25. mo.main()
  26. print( "耗时:", time.time() - t0)

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

安装: pip install python-docx

 


转载:https://blog.csdn.net/qq_34218221/article/details/106850560
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场