最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python爬取網(wǎng)頁內(nèi)容轉(zhuǎn)換為PDF文件

 更新時間:2020年07月28日 15:32:53   作者:007與狼共舞  
這篇文章主要為大家詳細介紹了python爬取網(wǎng)頁內(nèi)容轉(zhuǎn)換為PDF文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python爬取網(wǎng)頁內(nèi)容轉(zhuǎn)換為PDF的具體代碼,供大家參考,具體內(nèi)容如下

將廖雪峰的學(xué)習(xí)教程轉(zhuǎn)換成PDF文件,代碼只適合該網(wǎng)站,如果需要其他網(wǎng)站的教程,可靠需要進行稍微的修改。

# coding=utf-8 
import os 
import re 
import time 
import pdfkit 
import requests 
from bs4 import BeautifulSoup 
from PyPDF2 import PdfFileMerger
import sys
reload(sys)
sys.setdefaultencoding('utf8')

html_template = """ 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
 <meta charset="UTF-8"> 
</head> 
<body> 
{content} 
</body> 
</html> 

""" 

#----------------------------------------------------------------------
def parse_url_to_html(url, name): 
 """ 
 解析URL,返回HTML內(nèi)容 
 :param url:解析的url 
 :param name: 保存的html文件名 
 :return: html 
 """ 
 try: 
  response = requests.get(url) 
  soup = BeautifulSoup(response.content, 'html.parser') 
  # 正文 
  body = soup.find_all(class_="x-wiki-content")[0] 
  # 標題 
  title = soup.find('h4').get_text() 

  # 標題加入到正文的最前面,居中顯示 
  center_tag = soup.new_tag("center") 
  title_tag = soup.new_tag('h1') 
  title_tag.string = title 
  center_tag.insert(1, title_tag) 
  body.insert(1, center_tag) 
  html = str(body) 
  # body中的img標簽的src相對路徑的改成絕對路徑 
  pattern = "(<img .*?src=\")(.*?)(\")" 

  def func(m): 
   if not m.group(3).startswith("http"): 
    rtn = m.group(1) + "http://www.liaoxuefeng.com" + m.group(2) + m.group(3) 
    return rtn 
   else: 
    return m.group(1)+m.group(2)+m.group(3) 
  html = re.compile(pattern).sub(func, html) 
  html = html_template.format(content=html) 
  html = html.encode("utf-8") 
  with open(name, 'wb') as f: 
   f.write(html) 
  return name 

 except Exception as e:
  print "解析錯誤!"

#----------------------------------------------------------------------
def get_url_list(): 
 """ 
 獲取所有URL目錄列表 
 :return: 
 """ 
 response = requests.get("http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000") 
 soup = BeautifulSoup(response.content, "html.parser") 
 menu_tag = soup.find_all(class_="uk-nav uk-nav-side")[1] 
 urls = [] 
 for li in menu_tag.find_all("li"): 
  url = "http://www.liaoxuefeng.com" + li.a.get('href') 
  urls.append(url) 
 return urls 

#----------------------------------------------------------------------
def save_pdf(htmls, file_name): 
 """ 
 把所有html文件保存到pdf文件 
 :param htmls: html文件列表 
 :param file_name: pdf文件名 
 :return: 
 """ 
 options = { 
  'page-size': 'Letter', 
  'margin-top': '0.75in', 
  'margin-right': '0.75in', 
  'margin-bottom': '0.75in', 
  'margin-left': '0.75in', 
  'encoding': "UTF-8", 
  'custom-header': [ 
   ('Accept-Encoding', 'gzip') 
  ], 
  'cookie': [ 
   ('cookie-name1', 'cookie-value1'), 
   ('cookie-name2', 'cookie-value2'), 
  ], 
  'outline-depth': 10, 
 } 
 pdfkit.from_file(htmls, file_name, options=options) 

#----------------------------------------------------------------------
def main(): 
 start = time.time() 
 file_name = u"liaoxuefeng_Python3_tutorial" 
 urls = get_url_list() 
 for index, url in enumerate(urls):
  parse_url_to_html(url, str(index) + ".html") 
 htmls =[] 
 pdfs =[] 
 for i in range(0,124): 
  htmls.append(str(i)+'.html') 
  pdfs.append(file_name+str(i)+'.pdf') 

  save_pdf(str(i)+'.html', file_name+str(i)+'.pdf') 

  print u"轉(zhuǎn)換完成第"+str(i)+'個html' 

 merger = PdfFileMerger() 
 for pdf in pdfs:
  merger.append(open(pdf,'rb'))
  print u"合并完成第"+str(i)+'個pdf'+pdf 

 output = open(u"廖雪峰Python_all.pdf", "wb") 
 merger.write(output) 

 print u"輸出PDF成功!" 

 for html in htmls: 
  os.remove(html) 
  print u"刪除臨時文件"+html 

 for pdf in pdfs: 
  os.remove(pdf) 
  print u"刪除臨時文件"+pdf 

 total_time = time.time() - start 
 print(u"總共耗時:%f 秒" % total_time)

#----------------------------------------------------------------------
def changeDir(dir_name):
 """
 目錄切換
 """
 if not os.path.exists(dir_name):
  os.mkdir(dir_name)

 os.chdir(dir_name)
#----------------------------------------------------------------------
if __name__ == '__main__':
 #存放文件的路徑
 dir_name = '/home/Python/Html' 
 changeDir(dir_name)
 main() 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中如何進行連乘計算

    python中如何進行連乘計算

    在本篇文章里小編給大家分享的是關(guān)于python連乘計算的代碼,有興趣的朋友們可以參考學(xué)習(xí)下。
    2020-05-05
  • Python爬蟲XPath解析出亂碼的問題及解決

    Python爬蟲XPath解析出亂碼的問題及解決

    這篇文章主要介紹了Python爬蟲XPath解析出亂碼的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 基于Python實現(xiàn)的ID3決策樹功能示例

    基于Python實現(xiàn)的ID3決策樹功能示例

    這篇文章主要介紹了基于Python實現(xiàn)的ID3決策樹功能,簡單描述了ID3決策樹的相關(guān)概念,并結(jié)合實例形式分析了Python實現(xiàn)ID3決策樹的具體定義與使用技巧,需要的朋友可以參考下
    2018-01-01
  • Python畫圖小案例之小雪人超詳細源碼注釋

    Python畫圖小案例之小雪人超詳細源碼注釋

    在看了很多Python教程之后,覺得是時候做點什么小項目來練練手了,于是想來想去,用python寫了一個小雪人,代碼注釋無比詳細清楚,快來看看吧
    2021-09-09
  • Python實現(xiàn)Sqlite將字段當(dāng)做索引進行查詢的方法

    Python實現(xiàn)Sqlite將字段當(dāng)做索引進行查詢的方法

    這篇文章主要介紹了Python實現(xiàn)Sqlite將字段當(dāng)做索引進行查詢的方法,涉及Python針對sqlite數(shù)據(jù)庫索引操作的相關(guān)技巧,需要的朋友可以參考下
    2016-07-07
  • Python模擬登入的N種方式(建議收藏)

    Python模擬登入的N種方式(建議收藏)

    這篇文章主要介紹了Python模擬登入的N種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • python實現(xiàn)身份證實名認證的方法實例

    python實現(xiàn)身份證實名認證的方法實例

    這篇文章主要給大家介紹了關(guān)于python實現(xiàn)身份證實名認證的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 源碼解析python中randint函數(shù)的效率缺陷

    源碼解析python中randint函數(shù)的效率缺陷

    這篇文章主要介紹了源碼解析python中randint函數(shù)的效率缺陷,通過討論?random?模塊的實現(xiàn),并討論了一些更為快速的生成偽隨機整數(shù)的替代方法展開主題,需要的盆友可以參考一下
    2022-06-06
  • Python pandas常用函數(shù)詳解

    Python pandas常用函數(shù)詳解

    這篇文章主要介紹了Python pandas常用函數(shù)詳解,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • 使用python把Excel中的數(shù)據(jù)在頁面中可視化

    使用python把Excel中的數(shù)據(jù)在頁面中可視化

    最近學(xué)習(xí)數(shù)據(jù)分析,感覺Python做數(shù)據(jù)分析真的好用,下面這篇文章主要給大家介紹了關(guān)于如何使用python把Excel中的數(shù)據(jù)在頁面中可視化的相關(guān)資料,需要的朋友可以參考下
    2022-03-03

最新評論

婺源县| 龙门县| 石首市| 繁昌县| 三门峡市| 抚顺市| 榕江县| 大洼县| 普兰县| 奇台县| 吴旗县| 同心县| 综艺| 乌兰察布市| 滦南县| 东平县| 承德县| 刚察县| 布拖县| 十堰市| 平谷区| 高陵县| 鱼台县| 林甸县| 邯郸市| 商城县| 迭部县| 荣成市| 灵武市| 河南省| 六盘水市| 弋阳县| 九寨沟县| 昌图县| 兴国县| 买车| 清远市| 格尔木市| 高淳县| 阳江市| 余庆县|