python使用selenium爬百度文库ppt并生成pdf

2018-11-20 03:25:31来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

---恢复内容开始---

详细的讲解我是写在另外一个网址:https://www.yuque.com/docs/share/aacfa45c-22c5-4ef6-be97-cd6849002274

有点尬尴,所以就.....

 在这里直接放下另外一个例子(《数学模型答案》)的代码

from selenium import  webdriver
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
import requests

class downloader:
    def __init__(self):
        self.browser =webdriver.Chrome()
        self.wait =wait = WebDriverWait(self.browser,3)
        self.i=0
        self.pattern =re.compile('.*?url\("(.*?)"\)',re.S)
       

    def __call__(self,url):
        self.download(url)
        while True:
            for i in self.parse_link():
                self.save(i)
            
            sub =self.browser.find_element_by_id('next-pageList-1')
            self.browser.execute_script("arguments[0].scrollIntoViewIfNeeded(true);",sub)
            sub.click()
            
                
        self.browser.quit()
        

    def download(self,url):
        self.browser.get(url)
        submit =self.wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="html-reader-go-more"]/div[2]/div[1]/span/span[1]')))
        self.browser.execute_script("arguments[0].scrollIntoViewIfNeeded(true);",submit)
        submit.click()


                    
    def parse_link(self):
        self.elem=self.wait.until(EC.presence_of_element_located((By.ID,'reader-container-inner-1')))
        for i in self.elem.find_elements_by_class_name('bd'):
            try:
                self.browser.execute_script("arguments[0].scrollIntoViewIfNeeded(true);",i)
                time.sleep(0.6)    
                i =i.find_element_by_class_name('reader-pic-item')
                        
                js=i.get_attribute('style')

                href =self.pattern.findall(js)
                yield href[0]
            except NoSuchElementException:
                continue

    def save(self,link):
        html =requests.get(link).content
        with open('{}.png'.format(self.i),'wb') as f:
            f.write(html)
        self.i +=1


D =downloader()
D('https://wenku.baidu.com/view/d86fe3436c175f0e7dd13731')

 

---恢复内容结束---

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:pickle

下一篇:关于Python3.6中Twisted模块安装的问题