import requests
from lxml import etree
import time
import os
def creatPath(path):
if not os.path.exists(path):
print("Creat path")
os.makedirs(path)
if __name__ == '__main__':
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
path = 'D:/ppt_download/'
creatPath(path)
page_num_max = 5
for page_num in range(page_num_max):
base_url ='https://www.ypppt.com/moban/list-{0}.html'.format(page_num)
base_text = requests.get(url=base_url,headers=headers).text
base_tree = etree.HTML(base_text)
detail_li_list = base_tree.xpath('//ul[@class="posts clear"]/li')
for li in detail_li_list:
time.sleep(0.1)
detail_url = 'https://www.ypppt.com/' + li.xpath('./a[2]/@href')[0]
detail_page = requests.get(url=detail_url,headers=headers).text
detail_tree = etree.HTML(detail_page)
inf_data = detail_tree.xpath('//div[@class="infoss"]')[0]
title = inf_data.xpath('./h1/text()')[0].encode('iso-8859-1').decode('utf-8')
dowmload_page_url = 'https://www.ypppt.com/' + inf_data.xpath('./div[@class="button"]/a/@href')[0]
dowmload_page = requests.get(url=dowmload_page_url,headers=headers).text
dowmload_tree = etree.HTML(dowmload_page)
dowmload_url = dowmload_tree.xpath('//ul[@class="down clear"]/li/a/@href')[0]
ppt_data = requests.get(url=dowmload_url).content
fp = open(path+title + '.zip','wb')
fp.write(ppt_data)
print(title)