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 中將二進制轉(zhuǎn)換為整數(shù)的多種方法
這篇文章主要介紹了Python 中將二進制轉(zhuǎn)換為整數(shù),Python 中提供了多種方式將二進制字符串轉(zhuǎn)換為整數(shù),其中包括使用 int() 函數(shù)、使用二進制前綴和使用 eval() 函數(shù),本文通過實例代碼講解的非常詳細,需要的朋友可以參考下2023-05-05
python實現(xiàn)對圖片進行旋轉(zhuǎn),放縮,裁剪的功能
今天小編就為大家分享一篇python實現(xiàn)對圖片進行旋轉(zhuǎn),放縮,裁剪的功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
解決python執(zhí)行較大excel文件openpyxl慢問題
這篇文章主要介紹了解決python執(zhí)行較大excel文件openpyxl慢問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python識別設(shè)備和操作系統(tǒng)神器device_detector使用探究
這篇文章主要介紹了Python識別設(shè)備和操作系統(tǒng)神器device_detector庫使用探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

