Python爬取PPT模板(requests+BeautifulSoup+多线程)

快到做毕业设计的时间了,得去找点好看的PPT模板了,在http://www.ypppt.com这个网站上发现了很多不错的PPT,但是PPT有喜欢的有不喜欢的,一个一个下载打开又太慢,于是乎想到使用爬虫帮我批量下载

// 通过pip install 安装下面的库
// 我们去爬取这个网页上的PPT http://www.ypppt.com
import urllib.request as req   //网页请求库
from bs4 import BeautifulSoup  //网页解析库
import requests
import os
import time
from multiprocessing import Pool #线程class PPT():# 整个爬虫的类def __init__(self):self.baseUrl = "http://www.ypppt.com/moban/"//伪造请求头self.header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\(KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}self.page_count = 0def getResponseContent(self, url):#获取页面请求信息try:req_str = req.Request(url, headers = self.header)response = req.urlopen(req_str, timeout = 10)except:print("请求失败")else:return response.read().decode('utf-8')def getPageInfo(self, url):#获取页面数量html = self.getResponseContent(url)soup = BeautifulSoup(html, 'html.parser')#HTMLParser可以接收相应的HTML内容,并进行解析,#遇到HTML的标签会自动调用相应的handler(处理方法)来处理,#用户需要自己创建相应的子类来继承HTMLParser,并且复写相应的handler方法page_info = soup.find('div', attrs={'class':'page-navi'})a_list = page_info.find_all('a')last_a = a_list[len(a_list) - 1]href = last_a['href']page_count = href.replace('list-','').replace('.html','')self.page_count = int(page_count)def spyder(self, url):#解析第一个页面html = self.getResponseContent(url)soup = BeautifulSoup(html, 'html.parser')divs = soup.find_all('div', attrs={'class':'wrapper'})div = divs[1] # 目标divul = div.find_all('ul')[3]li_list =  ul.find_all('li')ppt_link_list = []for li in li_list:aTag_href = li.find_all('a')[1]['href']ppt_link = "http://www.ypppt.com" + aTag_href            ppt_link_list.append(ppt_link)pool = Pool(processes = 4)#利用multiprocessing中的Process动态成生多个进程pool.map(self.PPT_info, ppt_link_list)def PPT_info(self, url):#PPT下载页面html = self.getResponseContent(url)soup = BeautifulSoup(html, 'html.parser')down_button = soup.find('a', attrs={'class':'down-button'})['href']down_url = "http://www.ypppt.com" + down_buttonself.DL_PPT(down_url)time.sleep(1)def DL_PPT(self, url):#下载PPT页面html = self.getResponseContent(url)soup = BeautifulSoup(html, 'html.parser')ul = soup.find('ul', attrs={'class':'down clear'})rar_link = ul.find_all('a')[0]['href']# 个别PPT的下载页面不是绝对地址,需要手动添加if rar_link.find('.com') > 0:passelse:rar_link = 'http://www.ypppt.com' + rar_linkppt_name = soup.find('h1').textif ppt_name.find('-') > 0:ppt_name = ppt_name.split('-')[0].strip()f = requests.get(rar_link, headers = self.header)with open(ppt_name +  '.rar', 'wb') as rar:rar.write(f.content)print(ppt_name, "下载完成...")if __name__ == "__main__":ppt = PPT()start_time = time.time()ppt.getPageInfo(ppt.baseUrl)ppt.spyder(ppt.baseUrl)for page in range(2, ppt.page_count + 1):url = ppt.baseUrl + "list-{}.html".format(page)ppt.spyder(url)time.sleep(1)print(url, "下载完成...")end_time = time.time()print("需要:", end_time - start_time)

就这样,爬到了很多的PPT,然后就可以慢慢挑选了

Python爬取PPT模板(requests+BeautifulSoup+多线程)相关推荐

  1. python 24位图转 8位_Python爬取PPT模板小工具下载-Python爬取PPT模板小工具免费版下载v1.0...

    由于很多PPT抓取工具都会因为版本问题无法使用,所以论坛大神就自己写了这款Python爬取PPT模板小工具,可以帮助用户轻松获取各种PPT模板,使用的时候注意一次只能下载一种类型.软件仅供交流学习,下 ...

  2. python爬取ppt代码_Python爬取PPT模板小工具

    由于很多PPT抓取工具都会因为版本问题无法使用,所以论坛大神就自己写了这款Python爬取PPT模板小工具,可以帮助用户轻松获取各种PPT模板,使用的时候注意一次只能下载一种类型.软件仅供交流学习,下 ...

  3. python爬取简历模板_python 爬取免费简历模板网站的示例

    代码 # 免费的简历模板进行爬取本地保存 # http://sc.chinaz.com/jianli/free.html # http://sc.chinaz.com/jianli/free_2.ht ...

  4. Python爬取ppt工作项目模板

    前言 ppt模板爬取,大约有一百多套工作项目ppt模板,需要的小伙伴可以通过以下程序来下载! (1)爬取程序 # author:爱分享的山哥 import requests from bs4 impo ...

  5. 函数头模板_Python新手爬虫,爬取PPT模板

    爬取网站:第一PPT(http://www.1ppt.com/)此网站真的良心大写的牛 先上最后成功的源码: import requests import urllib import os from ...

  6. python爬取小说写入txt_Python BeautifulSoup 爬取笔趣阁所有的小说

    这是一个练习作品.用python脚本爬取笔趣阁上面的免费小说. 环境:python3 类库:BeautifulSoup 数据源:http://www.biqukan.cc 原理就是伪装正常http请求 ...

  7. Python爬取百度百科,BeautifulSoup提取关键信息

    本文主要爬取演员杨幂的百度百科,用到的python库有:requests和BeautifulSoup 主要内容共分为以下两个方面: 1. 用requests爬取网页内容 2. 用BeautifulSo ...

  8. 爬虫:python爬取简历模板

    import requests from lxml import etree import os # 分页: if __name__ == "__main__":if not os ...

  9. python爬ppt和风景_Python小工具爬取PPT模板

    报错,求楼主解决,谢谢 D:\pptdown目录已存在 Traceback (most recent call last): File "site-packages\urllib3\conn ...

最新文章

  1. python初学之魔法方法1
  2. repeater填充html,使用动态绑定到对象列表的ASP.NET Repeater创建HtmlTable
  3. clousx6机器人怎么导入词库_全国二等奖,临安学子登上机器人大赛国家级舞台...
  4. 毕业季offer怎么拿?收下这份非典型求职面试指南
  5. python 没有控件_PyQt自定义控件未显示
  6. 如何通过“限制管理权限”来保护您的企业?
  7. RNA 22. SCI 文章中基于表达估计恶性肿瘤组织的基质细胞和免疫细胞(ESTIMATE)
  8. Faster RCNN 中的Anchor
  9. 无聊写个示例程序,改自身PEB欺骗SREng
  10. kodi奈飞固件_在Ubuntu,Debian和Kodi上,如何安装Netflix
  11. 大学生静态HTML网页源码——佛山旅游景点介绍网页代码 家乡旅游网页制作模板 web前端期末大作业
  12. 【米勒拉宾模板】Palindromic Primes
  13. mysql自制食物_近2千条日常食物营养表ACCESS数据库Mysql数据库
  14. 老猿学5G专栏完结说明
  15. 数字时代:DevOps工程师的必备技能清单
  16. slice与splice用法
  17. 自学编程容易可行吗?
  18. 计算机一级ms必考知识点,计算机一级MsOffice考试知识点整理重点.docx
  19. MyEclipse设置黑色主题皮肤(详细)
  20. 饿了么的PWA升级实践

热门文章

  1. 【软考系统架构设计师】2017下系统架构师综合知识历年真题
  2. 新歌及经典歌曲在线听
  3. 房贷利率要不要转换,选LPR还是固定利率?这样思考就不会再迷茫
  4. C++ 单例设计模式
  5. 企业中常见的ERP运维问题!
  6. 软件工程:7.生存周期过程和软件生存周期模型
  7. 第一天来到新公司的volg (ETL开发工程师)
  8. 基于asp.net的员工出差管理信息系统#毕业设计#课程设计
  9. c语言学习一 (c语言格式)
  10. JavaScript Basics