python爬取微信公眾號文章圖片并轉(zhuǎn)為PDF
遇到那種有很多圖的微信公眾號文章咋辦?一個一個存很麻煩,應(yīng)朋友的要求自己寫了個爬蟲。
2.0版本完成了!完善了生成pdf的功能,可根據(jù)圖片比例自動調(diào)節(jié)大小,防止超出頁面范圍,增加了序號方面查看
#-----------------settings--------------- #url='https://mp.weixin.qq.com/s/8JwB_SXQ-80uwQ9L97BMgw' print('jd3096 for king 2.0 VIP8鉆石永久會員版') print('愿你遠離流氓軟件每一天') url=input('請輸入網(wǎng)址:') #-----------------get data---------------- import requests import re from bs4 import BeautifulSoup import os from PIL import Image try: os.makedirs('pics') except: pass os.chdir('pics') page=requests.get(url).text soup = BeautifulSoup(page, 'html.parser') jdata = soup.find_all('img') pn=0 for i in jdata: try: src=i['data-src'] print(src) rp = requests.get(src) with open(str(pn)+'.jpg','wb+')as f : # 循環(huán)寫入圖片 print(str(pn)+'.jpg') f.write(rp.content) pn+=1 except: pass #--------------------make pdf-------------------- from fpdf import FPDF import os path=os.getcwd() print(path) pdf = FPDF() pdf.set_auto_page_break(1) imagelist = [i for i in os.listdir()] imagelist.sort(key=lambda x: int(x.split('.')[0])) print(imagelist) for image in imagelist: try: img = Image.open(image) w = img.width #圖片的寬 h = img.height #圖片的高 ii=h/w print(ii) if ii>1.41: ww=int(250/ii) pdf.add_page() pdf.set_xy(0,0) pdf.set_font('arial','B',14) pdf.cell(60) pdf.cell(70,10,image,border=0, ln=1, align='C') pdf.image(os.path.join(path, image), w=ww, h=250) else: hh=int(180*ii) pdf.add_page() pdf.set_xy(0,0) pdf.set_font('arial','B',14) pdf.cell(60) pdf.cell(70,10,image,border=0, ln=1, align='C') pdf.image(os.path.join(path, image), w=180, h=hh) except: pass pdf.output(os.path.join(path, "merge.pdf"), "F")
爬完了長這樣:
PDF長這樣,比例適中適合閱讀
到此這篇關(guān)于python爬取微信公眾號文章圖片并轉(zhuǎn)為PDF的文章就介紹到這了,更多相關(guān)python微信公眾號文章圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python selenium 實例之通過 selenium 查詢禪道是否有任務(wù)或者BUG
這篇文章主要介紹了Python selenium 實例之通過 selenium 查詢禪道是否有任務(wù)或者BUG的相關(guān)資料,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09selenium與xpath之獲取指定位置的元素的實現(xiàn)
這篇文章主要介紹了selenium與xpath之獲取指定位置的元素的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python實現(xiàn)人人自動回復(fù)、搶沙發(fā)功能
這篇文章主要為大家詳細介紹了python實現(xiàn)人人自動回復(fù)、搶沙發(fā)功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Python使用SQLite和Excel操作進行數(shù)據(jù)分析
這篇文章主要介紹了Python使用SQLite和Excel操作進行數(shù)據(jù)分析,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01Python astype(np.float)函數(shù)使用方法解析
這篇文章主要介紹了Python astype(np.float)函數(shù)使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Python使用shelve模塊實現(xiàn)簡單數(shù)據(jù)存儲的方法
這篇文章主要介紹了Python使用shelve模塊實現(xiàn)簡單數(shù)據(jù)存儲的方法,涉及shelve模塊實現(xiàn)數(shù)據(jù)存儲的技巧,需要的朋友可以參考下2015-05-05