1 Star 0 Fork 0

ycs/doc_downloader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
book118_PPT.py 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
import base64
import time
import sys
import os
import shutil
from tqdm import trange
from img2pdf import conpdf
from PIL import Image
def download(url):
option = webdriver.ChromeOptions()
# option.add_argument('headless')
option.add_argument('log-level=3')
driver = webdriver.Chrome(
executable_path='.//chromedriver', chrome_options=option)
title = "output"
try:
driver.set_page_load_timeout(15)
driver.get(url)
title = driver.title
except:
print("Timeout - start download anyway.")
print(f'原创力: 《{title}》')
time.sleep(5)
driver.find_element_by_id('btn_preview_remain').click()
time.sleep(2)
frame = driver.find_elements_by_class_name('preview-iframe')[0]
src = frame.get_attribute('src')
print(src)
driver.get(src)
time.sleep(5)
if os.path.exists(f'./temp/{title}'):
shutil.rmtree(f'./temp/{title}')
os.makedirs(f'./temp/{title}')
pageCount = int(driver.find_element_by_id(
'PageCount').get_attribute('innerHTML'))
for i in trange(pageCount):
driver.save_screenshot(f'temp/{title}/capture.png')
page = driver.find_element_by_id('ppt')
left = page.location['x']
top = page.location['y']
right = left + page.size['width']
bottom = top + page.size['height'] - 35
im = Image.open(f'temp/{title}/capture.png')
im = im.crop((left, top, right, bottom)) # 元素裁剪
im.save(f'temp/{title}/{i}.png') # 元素截图
driver.find_element_by_id('pageNext').click()
time.sleep(1) # 防止还没加载出来
os.remove(f'./temp/{title}/capture.png')
driver.quit()
print('下载完毕,正在转码')
conpdf(f'output/{title}.pdf', f'temp/{title}', '.png')
if __name__ == '__main__':
download("https://max.book118.com/html/2019/1002/8052020057002053.shtm")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yang-doctor/doc_downloader.git
git@gitee.com:yang-doctor/doc_downloader.git
yang-doctor
doc_downloader
doc_downloader
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385