Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 国产视频一区在线观看,免费一区二区三区四区五区 ,91免费在线

          整合營銷服務(wù)商

          電腦端+手機端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          用python實現(xiàn)博主全部博文下載,制作成PDF電子

          用python實現(xiàn)博主全部博文下載,制作成PDF電子書籍

          python實現(xiàn)csdn博主全部博文下載,html轉(zhuǎn)pdf,有了學(xué)習(xí)的電子書了。。。(附源碼)

          我們學(xué)習(xí)編程,在學(xué)習(xí)的時候,會有想把有用的知識點保存下來,我們可以把知識點的內(nèi)容爬下來轉(zhuǎn)變成pdf格式,方便我們拿手機可以閑時翻看,是很方便的

          先來一個單個的博文下載轉(zhuǎn)pdf格式的操作

          私信小編01即可獲取大量Python學(xué)習(xí)資源

          python中將html轉(zhuǎn)化為pdf的常用工具是Wkhtmltopdf工具包,在python環(huán)境下,pdfkit是這個工具包的封裝類。如何使用pdfkit以及如何配置呢?分如下幾個步驟。

          下載wkhtmltopdf安裝包,并且安裝到電腦上。

          我下的是這個版本,安裝的時候要記住路徑,之后調(diào)用要用到路徑

          開發(fā)工具

          python
          pycharm
          pdfkit (pip install pdfkit)
          lxml

          今天目標(biāo):博主的全部博文下載,并且轉(zhuǎn)pdf格式保存

          基本思路:
          1、url + headers
          2、分析網(wǎng)頁: CSDN網(wǎng)頁是靜態(tài)網(wǎng)頁, 請求獲取網(wǎng)頁源代碼
          3、lxml解析獲取boke_urls, author_name
          4、循環(huán)遍歷,得到 boke_url

          5、xpath解析獲取文件名
          6、css選擇器獲取標(biāo)簽文本的主體
          7、構(gòu)造拼接html文件
          8、保存html文件
          9、文件的轉(zhuǎn)換

          分析網(wǎng)頁: CSDN網(wǎng)頁是靜態(tài)網(wǎng)頁, 請求獲取網(wǎng)頁源代碼

          start_url=“https://i1bit.blog.csdn.net/”

          為例
          確定網(wǎng)址為同步加載

          css選擇器獲取標(biāo)簽文本的主體為代碼要點部分
          css語法部分

          	html_css=parsel.Selector(響應(yīng)的數(shù)據(jù))
              html_content=html_css.css('要獲取的部分').get()
          

          點開博主的一篇博文打開開發(fā)者工具

          # css選擇器獲取標(biāo)簽文本的主體
                  html_css=parsel.Selector(response_2)
                  html_content=html_css.css('article').get()
          # 構(gòu)造拼接html文件
                  html=\
                      '''
                          <!DOCTYPE html>
                              <html lang="en">
                              <head>
                                  <meta charset="UTF-8">
                                  <title>Title</title>
                              </head>
                              <body>
                                  {}
                              </body>
                          </html>
                      '''.format(html_content)
          

          文件的轉(zhuǎn)換

             config=pdfkit.configuration(wkhtmltopdf=r'這里為下載wkhtmltopdf.exe的路徑')
                      pdfkit.from_file(
                          第一個參數(shù)要轉(zhuǎn)變的html文件,
                          第二個參數(shù)轉(zhuǎn)變后的pdf文件,
                          configuration=config
                      ) 
                      # 上面這樣寫清楚一點,也可以直接
                      pdfkit.from_file(
                          第一個參數(shù)要轉(zhuǎn)變的html文件,
                          第二個參數(shù)轉(zhuǎn)變后的pdf文件,
                      configuration=pdfkit.configuration(wkhtmltopdf=r'這里為下載wkhtmltopdf.exe的路徑')
                      )
          

          源碼展示:

          import parsel, os, pdfkit
          from lxml import etree
          from requests_html import HTMLSession
          session=HTMLSession()
          
          
          
          def main():
              # 1、url + headers
              start_url=input(r'請輸入csdn博主的地址:')
              headers={
                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                                '(KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
              }
          
              # 2、分析網(wǎng)頁: CSDN網(wǎng)頁是靜態(tài)網(wǎng)頁, 請求獲取網(wǎng)頁源代碼
              response_1=session.get(start_url, headers=headers).text
          
          
              # 3、解析獲取boke_urls, author_name
              html_xpath_1=etree.HTML(response_1)
          
              author_name=html_xpath_1.xpath(r'//*[@id="floor-user-profile_485"]/div/div[1]/div[2]/div[2]/div[1]/div[1]/text()')[0]
          
              boke_urls=html_xpath_1.xpath(r'//article[@class="blog-list-box"]/a/@href')
          
          
              # 4、循環(huán)遍歷,得到 boke_url
              for boke_url in boke_urls:
          
                  # 5、請求
                  response_2=session.get(boke_url, headers=headers).text
          
                  # 6、xpath解析獲取文件名
                  html_xpath_2=etree.HTML(response_2)
                  file_name=html_xpath_2.xpath(r'//h1[@id="articleContentId"]/text()')[0]
          
          
                  # 7、css選擇器獲取標(biāo)簽文本的主體
                  html_css=parsel.Selector(response_2)
                  html_content=html_css.css('article').get()
          
                  # 8、構(gòu)造拼接html文件
                  html=\
                      '''
                          <!DOCTYPE html>
                              <html lang="en">
                              <head>
                                  <meta charset="UTF-8">
                                  <title>Title</title>
                              </head>
                              <body>
                                  {}
                              </body>
                          </html>
                      '''.format(html_content)
          
                  # 9、創(chuàng)建兩個文件夾, 一個用來保存html 一個用來保存pdf文件
                  if not os.path.exists(r'{}-html'.format(author_name)):
                      os.mkdir(r'{}-html'.format(author_name))
          
                  if not os.path.exists(r'{}-pdf'.format(author_name)):
                      os.mkdir(r'{}-pdf'.format(author_name))
          
                  # 10、保存html文件
                  try:
                      with open(r'{}-html/{}.html'.format(author_name, file_name), 'w', encoding='utf-8') as f:
                          f.write(html)
                  except Exception as e:
                      print('文件名錯誤')
          
                  # 11、文件的轉(zhuǎn)換
                  try:
                      config=pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
                      pdfkit.from_file(
                          '{}-html/{}.html'.format(author_name, file_name),
                          '{}-pdf/{}.pdf'.format(author_name, file_name),
                          configuration=config
                      )
                      a=print(r'--文件下載成功:{}.pdf'.format(file_name))
          
                  except Exception as e:
                      continue
          
          
          if __name__=='__main__':
             main()
          
          

          代碼操作:

          器之心報道

          項目作者:vinayak mehta參與:一鳴

          從 PDF 表格中獲取數(shù)據(jù)是一項痛苦的工作。不久前,一位開發(fā)者提供了一個名為 Camelot 的工具,使用三行代碼就能從 PDF 文件中提取表格數(shù)據(jù)。

          PDF 文件是一種非常常用的文件格式,通常用于正式的電子版文件。它能夠很好的將不同的排版格式固定下來,形成版面清晰且美觀的展示效果。然而,對于想要從 PDF 中提取信息的人們來說,PDF 是個噩夢,尤其是表格。

          大量的學(xué)術(shù)報告、論文、分析文章都使用 PDF 展示其中的表格數(shù)據(jù),但是對于如果想要直接從表格中復(fù)制數(shù)據(jù)則會非常麻煩。不久前,有一位開發(fā)者提供了一個可從文字 PDF 中提取表格信息的工具——Camelot,能夠直接將大部分表格轉(zhuǎn)換為 Pandas 的 Dataframe。

          • 項目地址:https://github.com/camelot-dev/camelot

          Camelot 是什么

          據(jù)項目介紹稱,Camelot 是一個 Python 工具,用于將 PDF 文件中的表格數(shù)據(jù)提取出來。

          具體而言,用戶可以像使用 Pandas 那樣打開 PDF 文件,然后利用這個工具提取表格數(shù)據(jù),最后再指定輸出的形式(如 csv 文件)。

          代碼示例

          項目提供的 PDF 文件如圖所示,假設(shè)用戶需要提取這些文字之間的表格 2-1 中的信息。

          PDF 文件。我們需要提取表格 2-1。

          使用 Camelot 提取表格數(shù)據(jù)的代碼如下:

          >>> import camelot
          >>> tables=camelot.read_pdf('foo.pdf') #類似于Pandas打開CSV文件的形式
          >>> tables[0].df # get a pandas DataFrame!
          >>> tables.export('foo.csv', f='csv', compress=True) # json, excel, html, sqlite,可指定輸出格式
          >>> tables[0].to_csv('foo.csv') # to_json, to_excel, to_html, to_sqlite, 導(dǎo)出數(shù)據(jù)為文件
          >>> tables
          <TableList n=1>
          >>> tables[0]
          <Table shape=(7, 7)> # 獲得輸出的格式
          >>> tables[0].parsing_report
          {
           'accuracy': 99.02,
           'whitespace': 12.24,
           'order': 1,
           'page': 1
          }
          

          以下為輸出的結(jié)果,對于合并的單元格,Camelot 在抽取后做了空行處理,這是一個穩(wěn)妥的方法。

          安裝方法

          項目作者提供了三種安裝方法。首先,你可以使用 Conda 進行安裝,這是最簡單的。

          conda install -c conda-forge camelot-py
          

          最流行的安裝方法是使用 pip 安裝。

          pip install camelot-py[cv]
          

          還可以從項目中克隆代碼,并使用源碼安裝。

          理:

          使用Selenium + ChromeDriver打開頁面, 拿到html 源碼,然后再使用pdfkit生成pdf

          概念:

          Selenium:Selenium是一個用于Web應(yīng)用程序測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣

          ChromeDriver:它是 google 為網(wǎng)站開發(fā)人員提供的自動化測試接口,是 selenium2 和 chrome瀏覽器 進行通信的橋梁

          wkhtmltopdf:它是一個適用于多平臺的html到pdf轉(zhuǎn)換的軟件

          pdfkit:它是wkhtmltopdf的Python封裝包

          安裝(Linux CentOS7)

          1、安裝wkhtmltopdf

          1)在wkhtmltopdf的官網(wǎng)下載安裝包

          官網(wǎng)地址:https://wkhtmltopdf.org/downloads.html

          2)上傳到linux服務(wù)器

          scp wkhtmltox-0.12.6-1.centos7.x86_64.rpm tn@10.211.55.22:~/soft/

          3)安裝

          sudo rpm -ivh wkhtmltox-0.12.6-1.centos7.x86_64.rpm

          報錯:

          錯誤:依賴檢測失敗:
              xorg-x11-fonts-75dpi 被 wkhtmltox-1:0.12.6-1.centos7.x86_64 需要

          安裝xorg-x11-fonts-75dpi:

          sudo yum install xorg-x11-fonts-75dpi

          再次安裝:

          sudo rpm -ivh wkhtmltox-0.12.6-1.centos7.x86_64.rpm

          安裝wkhtmltopdf

          4)在/usr/local/bin/目錄下可以看到安裝了wkhtmltoimage 和 wkhtmltopdf

          查看安裝結(jié)果

          5)測試是否安裝成功

          wkhtmltopdf http://www.baidu.com ~/test.pdf

          執(zhí)行成功后,打開test.pdf,看到下面效果,說明安裝成功

          wkhtmltopdf安裝成功

          2、安裝pdfkit

          1)安裝Python環(huán)境

          使用Anconada安裝Python環(huán)境:

          conda create --name python3.6.6 python=3.6.6

          進入Python 3.6.6環(huán)境(后續(xù)所有命令都是在此環(huán)境下操作):

          source activate python3.6.6

          2)安裝pdfkit

          pip install pdfkit

          3)示例

          import pdfkit
          
          pdf_options={
              'page-size': 'A4',
          }
          
          url='https://www.baidu.com/'
          
          pdfkit.from_url(url, 'test.pdf', options=pdf_options)

          打開test.pdf看到baidu頁面,說明pdfkit安裝成功


          3、為什么要使用Selenium + ChromeDriver

          以上的方法僅適用于靜態(tài)頁,如果頁面是動態(tài)加載出來的,使用以上方法會有問題,動態(tài)調(diào)用后端接口的部分會加載不出來

          為了解決這個問題,可以使用Selenium + ChromeDriver打開頁面,拿到頁面源碼,然后再使用pdfkit生成pdf


          4、安裝ChromeDriver

          ChromeDriver是依賴于Chrome運行的,所以需要先安裝Chrome

          1)安裝Chrome

          請自行搜索

          2)安裝ChromeDriver

          下載地址:http://chromedriver.storage.googleapis.com/index.html

          ChromeDriver的版本號需要跟Chrome的版本號對應(yīng)

          比如我的Chrome版本號為Google Chrome 86.0.4240.183,那么對應(yīng)的ChromeDriver版本號也是86


          點擊進入,查看notes.txt,可以看到這個版本的ChromeDriver對應(yīng)的Chrome版本號

          找到相應(yīng)的系統(tǒng)版本下載:

          scp到服務(wù)器:

          scp ~/Downloads/chromedriver_linux64.zip tn@10.211.55.22:~/soft/

          解壓:

          unzip chromedriver_linux64.zip

          移動:

          sudo mv chromedriver /usr/local/bin/chromedriver

          查看chromedriver版本:

          chromedriver --version

          顯示版本:ChromeDriver 86.0.4240.22


          5、安裝selenium

          pip install selenium

          示例

          代碼示例:

          import pdfkit, time, pprint
          from selenium import webdriver
          
          options_chrome=webdriver.ChromeOptions()
          # 以最高權(quán)限運行
          options_chrome.add_argument('--no-sandbox')
          # 瀏覽器不提供可視化頁面,linux下如果系統(tǒng)不支持可視化不加這條會啟動失敗
          options_chrome.add_argument('--headless')
          # executable_path為chromedriver的位置
          driver=webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', chrome_options=options_chrome)
          # 瀏覽器全屏
          driver.fullscreen_window()
          
          url='http://www.tn666.com/test?id=1'
          driver.get(url)
          # sleep 1秒
          time.sleep(1)
          source_text=driver.page_source
          
          options_pdf={
              'page-size': 'A4'
          }
          result=pdfkit.from_string(source_text, 'test.pdf', options=options_pdf)
          
          driver.quit()

          請將url換為您想轉(zhuǎn)為pdf的url


          若覺得對您有所幫助,請幫忙點個贊,謝謝~


          主站蜘蛛池模板: 久久精品国产一区二区| 久久精品免费一区二区喷潮| 亚洲国产精品无码第一区二区三区| 亚洲一区AV无码少妇电影| 高清一区二区三区日本久| 色视频综合无码一区二区三区 | 精品无码人妻一区二区免费蜜桃| eeuss鲁片一区二区三区| 毛片一区二区三区| 韩国福利影视一区二区三区| 久久综合九九亚洲一区| 精品一区二区三区在线视频| 精品国产一区二区三区久久蜜臀 | 无码精品一区二区三区在线| 国产精品99无码一区二区| 亚洲国产精品第一区二区三区| 福利一区福利二区| 冲田杏梨高清无一区二区| 区三区激情福利综合中文字幕在线一区亚洲视频1| 亚洲av综合av一区二区三区| 中文字幕在线不卡一区二区| 亚洲日韩国产欧美一区二区三区| 国产精品久久一区二区三区 | 亚洲一区二区三区偷拍女厕| 中文字幕一区二区三区四区| 日韩综合无码一区二区| 无码囯产精品一区二区免费| 日韩人妻无码一区二区三区99 | 国产萌白酱在线一区二区| 国产免费av一区二区三区| 色视频综合无码一区二区三区| 国产成人精品一区二区三区| 久久精品一区二区| 精品熟人妻一区二区三区四区不卡| 亚洲无圣光一区二区| 国产精品一区二区四区| 中文字幕在线观看一区二区三区| 无码国产精成人午夜视频一区二区| 国产精品揄拍一区二区久久| 日韩精品一区二区三区国语自制 | 日韩一区二区视频在线观看 |