python爬取微信公眾號文章圖片并轉為PDF
更新時間:2022年02月08日 14:32:01 作者:三十歲開始學編程的大叔
大家好,本篇文章主要講的是python爬取微信公眾號文章圖片并轉為PDF,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
遇到那種有很多圖的微信公眾號文章咋辦?一個一個存很麻煩,應朋友的要求自己寫了個爬蟲。
2.0版本完成了!完善了生成pdf的功能,可根據圖片比例自動調節(jié)大小,防止超出頁面范圍,增加了序號方面查看
#-----------------settings---------------
#url='https://mp.weixin.qq.com/s/8JwB_SXQ-80uwQ9L97BMgw'
print('jd3096 for king 2.0 VIP8鉆石永久會員版')
print('愿你遠離流氓軟件每一天')
url=input('請輸入網址:')
#-----------------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長這樣,比例適中適合閱讀

到此這篇關于python爬取微信公眾號文章圖片并轉為PDF的文章就介紹到這了,更多相關python微信公眾號文章圖片內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)對圖片進行旋轉,放縮,裁剪的功能
今天小編就為大家分享一篇python實現(xiàn)對圖片進行旋轉,放縮,裁剪的功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
解決python執(zhí)行較大excel文件openpyxl慢問題
這篇文章主要介紹了解決python執(zhí)行較大excel文件openpyxl慢問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python識別設備和操作系統(tǒng)神器device_detector使用探究
這篇文章主要介紹了Python識別設備和操作系統(tǒng)神器device_detector庫使用探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

