儲存CSDN 中的部落格文章為本地檔案
儲存CSDN 中的部落格文章為本地檔案
2019年3月31日21:49:39 【原創】
python 學習部落格目錄
1. 執行環境
最近發現我CSDN裡的部落格中,外鏈的圖片全部無法載入,部落格總使用的圖片論壇是 https://i.imgur.com/
,圖片連結形如 https://i.imgur.com/WxbYfSu.png
,圖片是使用 markdown 軟體寫部落格時上傳的。
目前在國內的 IP 無法訪問 i.imgur.com
網站,不知道啥情況。
現在的問題是國內的IP無法訪部落格裡的圖片,所以決定掛個國外的代理將圖片儲存至本地,以後有時間再寫個指令碼再批量替換,畢竟部落格數量還是很多的。
有幾點不足:
- 未下載 css 和 js 檔案,其實實現起來很簡單,可以複製程式碼中的正則自己擴充套件。
- 下載後的HTML檔案中的js和css都是需要聯網,不聯網只能看到文章內容,不能看到美化佈局。
廢話不多說,環境是 python3 的,沒有多餘的庫需要安裝,忘記是不是需要 pillow 庫了。
pip install Pillow
2. 流程分解
-
URL儲存在檔案中
-
getHtml() 函式,作用是掛代理訪問 URL;
def getHtml(url): proxies = {"https": "127.0.0.1:1080"} response = requests.get(url, proxies = proxies) return response.text
-
getImg() 函式,作用是格式化過濾儲存文章內容為圖片;
# 過濾自動跳轉 pattern1 = re.compile(r'<div style="display:none;">(.*?)</div>',flags=re.S) match1 = pattern1.sub('',body) #print(match1)
-
頁面中的title作為檔名,同時也是路徑名;
# 獲取title作為檔名使用 title = '' title_list = re.findall('<title>(.*?) - kevinhanser - CSDN部落格</title>',match4) #print(title_list) title = str(title_list[0]) print(title)
-
將body中的關於圖片的路徑連結都替換為本地的圖片路徑,使在本地檢視文章時也可以載入圖片;
# 定位body中的圖片位置 #match5 = pattern5.findall(match4) pattern5 = re.compile(r'<p>(.*)i.imgur.com') # 替換body中的圖片檔名為本地的(./image/+title) match5 = pattern5.sub('<img src="'+'./image/'+title,match4)
-
for 迴圈中是將圖片名使用正則匹配出來,然後構造完整的圖片連結下載並儲存為同名檔案。
# 儲存圖片,將img_url儲存為img_path,儲存為本地檔案 if not os.path.exists(img_path): with urllib.request.urlopen(img_url, timeout=120) as response, open(img_path, 'wb') as f_save: f_save.write(response.read()) f_save.flush() f_save.close()
3. 程式碼部分
#coding=utf-8
import urllib #urllib模組提供了讀取Web頁面資料的介面.
import os # 系統相關
import re # 正規表示式
from PIL import Image
import os,stat
import urllib.request
import requests
import time
# python3
def getHtml(url):
proxies = {"https": "127.0.0.1:1080"}
response = requests.get(url, proxies = proxies)
return response.text
def getImg(body):
# 過濾自動跳轉
pattern1 = re.compile(r'<div style="display:none;">(.*?)</div>',flags=re.S)
match1 = pattern1.sub('',body)
#print(match1)
# 過濾推薦及廣告
pattern2 = re.compile(r'</article>(.*)</script>',flags=re.S)
match2 = pattern2.sub('</div>',match1)
#print(match2)
# 過濾CSDN的最上頭的橫幅
pattern2_tmp = re.compile(r'b site By baidu end\n </script>\n (.*?) <script src="https://csdnimg.cn/rabbit/exposure-click/main-1.0.6.js"></script>',flags=re.S)
match2_tmp = pattern2_tmp.sub('',match2)
#print(match2_tmp)
# 過濾css,使頁面全屏
pattern3 = re.compile(r'https://csdnimg.cn/release/phoenix/themes/skin-yellow/skin-yellow-2eefd34acf.min.css',flags=re.S)
match3 = pattern3.sub('',match2_tmp)
#print(match3)
# 過濾使頁面最大,而不是居中固定寬度
pattern4 = re.compile(r'container',flags=re.S)
match4 = pattern4.sub('container_bak',match3)
#print(match4)
#container
# 獲取title作為檔名使用
title = ''
title_list = re.findall('<title>(.*?) - kevinhanser - CSDN部落格</title>',match4)
#print(title_list)
title = str(title_list[0])
print(title)
# python2中使用.decode('string_escape')編碼為中文
#title = str(title_list[0]).decode('string_escape')
# 獲取圖片名
picture_list = re.findall('https://i.imgur.com/(.*?).png',match4)
picture = picture_list
#print picture
# 定位body中的圖片位置
#match5 = pattern5.findall(match4)
pattern5 = re.compile(r'<p>(.*)i.imgur.com')
# 替換body中的圖片檔名為本地的(./image/+title)
match5 = pattern5.sub('<img src="'+'./image/'+title,match4)
# 將body寫成檔案,編碼為中文
f = open(title+'.html','w',encoding='utf8')
f.write(match5)
f.close()
#部落格中無圖片則提示
if len(picture) == 0:
print("%s 中無圖片" % title)
#部落格中有圖片則儲存,使用title作為目錄路徑名
for i in range(len(picture)):
if not os.path.exists('./image/'+title):
os.makedirs('./image/'+title)
#print picture[i]
img_path = './image/'+title+'/'+picture[i]+'.png'
img_url = 'https://i.imgur.com/'+picture[i]+'.png'
print("%s 開始..." % img_url)
#print(img_path)
# 儲存圖片,將img_url儲存為img_path,儲存為本地檔案
if not os.path.exists(img_path):
with urllib.request.urlopen(img_url, timeout=120) as response, open(img_path, 'wb') as f_save:
f_save.write(response.read())
f_save.flush()
f_save.close()
# 請求過快會報錯,前面有重試機制
#time.sleep(15)
print(picture[i]+'.png'+"圖片載入完成")
if __name__ == '__main__':
# kali.txt 文件中存放的是CSDN部落格的URL連結
f = open("kali.txt","r")
URL_list = f.readlines()
f.close()
print(URL_list)
for url in URL_list:
# 設定錯誤重試機制
attempts = 0
success = False
print(url)
while attempts < 30 and not success:
try:
# 呼叫函式,主要就這兩個
body = getHtml(url.strip("\n")) # 獲取頁面body
getImg(body) # 對body進行操作
success = True
except:
attempts += 1
# 重試超時
if attempts % 5 == 0:
time.sleep(10)
elif attempts == 30:
break
相關文章
- 儲存檔案到本地
- 在.Net中將MailMessage儲存為本地eml檔案(轉)AI
- 部落格一鍵儲存本地exe視覺化介面檔案視覺化
- Android從外部儲存裝置中儲存和載入本地檔案Android
- Android中的資料儲存之檔案儲存Android
- 塊儲存 檔案儲存 物件儲存物件
- Matlab將.mat檔案儲存為.txt檔案Matlab
- vista alv儲存本地檔案錯誤
- spark在IDEA的本地無法使用saveAsTextFile儲存檔案SparkIdeaAST
- 檔案儲存
- 本地儲存
- Android下載網路pdf檔案儲存至本地Android
- IPFS的檔案儲存模式模式
- CIFS檔案的儲存————SMB
- 自己的iteye的部落格文章合集(pdf)
- 本地儲存-系統和保留-系統檔案佔用儲存空間過大的解決方式
- Flutter持久化儲存之檔案儲存Flutter持久化
- JavaScript 本地儲存JavaScript
- web本地儲存Web
- angular 本地儲存Angular
- 如何將html程式碼儲存為Pdf檔案HTML
- 聊聊前端的本地儲存前端
- 在vue中,localStorage本地儲存應用。Vue
- 簡單介紹nacos單機本地配置檔案儲存位置方式
- 本地MinIO儲存服務Java遠端呼叫上傳檔案Java
- BIEE 儲存本地自定義檔案並通過fmap函式使用函式
- C#開發中使用配置檔案物件簡化配置的本地儲存C#物件
- Android 檔案儲存Android
- hive檔案儲存格式Hive
- InnoDB儲存引擎檔案儲存引擎
- 06、hdfs檔案儲存
- android: 檔案儲存Android
- 物件儲存 vs 檔案儲存 vs 塊儲存,選哪個?物件
- javascript如何將檔案儲存到本地JavaScript
- RAC歸檔配置方案:使用NFS共享儲存儲存歸檔檔案NFS
- PHP匯出大量資料,儲存為CSV檔案PHP
- 如何將MAC的檔案儲存至NAS網路儲存?Mac
- 本地儲存localStorage使用