1 Star 5 Fork 2

weder/doc_downloader_web

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
jinchutou.py 3.03 KB
一键复制 编辑 原始数据 按行查看 历史
豆腐 提交于 2020-02-10 17:09 +08:00 . init commit
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
import requests
from tqdm import trange
from img2pdf import conpdf
def download(url, callback):
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('log-level=3')
driver = webdriver.Chrome(chrome_options=option)
title = "output"
try:
# driver.implicitly_wait(15)
driver.set_page_load_timeout(15)
driver.get(url)
title = driver.title
except Exception as e:
print("Timeout - start download anyway.")
return False, "下载失败,超时"
print(title)
if os.path.exists(f'./output/{title}.pdf'):
return True, title
time.sleep(5)
while True:
try:
# 展开全部
elem_cont_button = driver.find_element_by_class_name(
"banner-download")
driver.execute_script(
"arguments[0].scrollIntoView(true);", elem_cont_button)
actions = ActionChains(driver)
actions.move_to_element(elem_cont_button).perform()
time.sleep(0.5)
driver.find_element_by_class_name("down-arrow").click()
except Exception:
break
try:
# 获取页数
num_of_pages = driver.find_element_by_id('readshop').find_element_by_class_name(
'mainpart').find_element_by_class_name('shop3').find_elements_by_class_name('text')[-1].get_attribute('innerHTML')
num_of_pages = int(num_of_pages.split(';')[-1])
if os.path.exists(f'./temp/{title}'):
shutil.rmtree(f'./temp/{title}')
os.makedirs(f'./temp/{title}')
for pages in trange(num_of_pages):
callback(pages, num_of_pages, "正在下载:%s" % title)
try:
time.sleep(0.5)
elem = driver.find_element_by_id(f'outer_page_{pages+1}')
actions = ActionChains(driver)
actions.move_to_element(elem).perform()
img = elem.find_element_by_tag_name('img')
img_url = img.get_attribute('src')
res = requests.get(img_url)
with open(f"./temp/{title}/{pages}.gif", "wb") as fh:
fh.write(res.content)
except Exception as e:
print("下载失败,可能由于文档为收费预览文档。错误信息:\n%r" % e)
break
driver.quit()
print('下载完毕,正在转码')
callback(99, 100, "正在转码")
conpdf(f'output/{title}.pdf', f'temp/{title}', '.gif')
return True, title
except Exception as e:
return False, e
if __name__ == '__main__':
download("https://www.jinchutou.com/p-120187856.html")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/changeWeder/doc_downloader_web.git
git@gitee.com:changeWeder/doc_downloader_web.git
changeWeder
doc_downloader_web
doc_downloader_web
master

搜索帮助