python爬取站长素材网页图片保存到ppt中
2018-07-17 17:41
246 查看
网站地址:http://sc.chinaz.com/tupian/index.html
直接上代码:
个人娱乐
直接上代码:
import requests from bs4 import BeautifulSoup from pptx import Presentation from pptx.util import Inches import os header = { "Referer":"http://sc.chinaz.com/tupian/index_2.html", "Upgrade-Insecure-Requests":1, "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36", "Host":"sc.chinaz.com", "If-None-Match":"b22f28e7941dd41:0", "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Connection":"keep-alive" } #创建ppt prs = Presentation() #图片放置位置 left, top, width, height = Inches(1), Inches(0.5), Inches(8), Inches(6) #查询最大页数 response = requests.get("http://sc.chinaz.com/tupian/index.html") soup = BeautifulSoup(response.text,"lxml"); b=soup.find_all("b") print("最大页数为%s" % b[-1].text) for i in range(2,int(b[-1].text)): print("下载第%d页的图片" % i) resposne = requests.get("http://sc.chinaz.com/tupian/index_%s.html" % str(i)); resposne.encoding="utf-8" soup = BeautifulSoup(resposne.text,"lxml") div = soup.find("div",id="container") #获取所有a标签 a = div.find_all("img") for j in a: #获取名称 name = j["alt"] #获取图片地址 img = j["src2"] #href = j.find("img")["src2"] #图片保存的名称 imgname = name+".jpg" conteng = requests.get(img) #图片二进制 er = conteng.content with open("F:\\BaiduNetdiskDownload\\"+imgname,"wb") as file: print("正在下载图片--%s" %imgname) file.write(er) file.flush() #创建一个空白ppt ppt = prs.slide_layouts[6] #添加到ppt中 ppts = prs.slides.add_slide(ppt) #空白ppt添加下载的图片 pic = ppts.shapes.add_picture("F:\\BaiduNetdiskDownload\\"+imgname, left, top, width, height) #删除图片 try: if os.path.exists("F:\\BaiduNetdiskDownload\\"+imgname): # 删除文件,可使用以下两种方法。 os.remove("F:\\BaiduNetdiskDownload\\"+imgname) # os.unlink(my_file) except: pass files= os.listdir("F:\\BaiduNetdiskDownload\\") if files: for k in files: print("正在删除图片--%s" % k) os.remove("F:\\BaiduNetdiskDownload\\"+k) prs.save("ppt1.ppt")
个人娱乐
相关文章推荐
- python连续爬取多个网页的图片分别保存到不同的文件夹
- python 网页爬虫+保存图片+多线程+网络代理
- python 网页抓取并保存图片
- Python保存一个网页的全部内容(包含链接内的图片等等)
- 黄聪:python访问抓取网页常用命令(保存图片到本地、模拟POST、GET、中文编码问题)
- python 网页爬虫+保存图片+多线程+网络代理
- python保存网页图片到本地的方法
- python 爬虫入门1 网页图片保存
- python爬虫实战(1)抓取网页图片自动保存
- selenium,phantomJS,python整合的一个小示例,把某个网页画面整体保存成图片格式。
- python抓取网页中图片并保存到本地
- python 解析网页,将抓取到的图片保存到本地
- python抓取网页中图片并保存到本地
- python抓取网页中图片并保存到本地
- python 网页爬虫+保存图片+多线程+网络代理
- 使用python 简单的保存网页的图片
- python抓取网页中图片并保存到本地
- python抓取网页中图片并保存到本地
- 使用Python保存网页上的图片或者保存页面为截图