Wechat article pictures downloader

2020-01-05 Views Software | Python334字2 min read
featureimg

This downloader can be used to download pictures from wechat article.

Preparation

  • Install reportlab and PyPDF2 in python (if you want to convert pictures into pdf)

Code

import re
import requests
import os
from os import remove,listdir 
from os.path import join 
from reportlab.lib.pagesizes import A4,landscape,portrait 
from reportlab.pdfgen import canvas 
from PyPDF2 import PdfFileReader,PdfFileMerger 

header={"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3464.0 Safari/537.36"}
l = input("请输入下载链接:")
daohang = requests.get(l,headers=header)
daohang.encoding = ('utf8')
da = re.findall('data-src="(.*?)"',daohang.text,re.S)
path = re.findall('<h2 class="rich_media_title" id="activity-name">(.*?)</h2>',daohang.text,re.S)
path = path[0].strip()
print("正在下载中:")
name = 1

for i in range(0,len(da)):
    print("正在下载第" + str(i) + "张图片")
    r=requests.get(da[i], headers=header)
    if not os.path.exists(path):
        os.makedirs(path)
    with open(path +"\\" + str(name) + ".jpeg",'wb') as code:
        code.write(r.content)
        code.close()
    name += 1

print("下载完成!\n")

judge = input("是否需要转成pdf:是按1,不是按任意键退出:")
if judge == "1":
    jpg_files = [path + "\\" + str(i)+".jpeg" for i in range(1,name)]
    result_pdf = PdfFileMerger()

    for fn in jpg_files:
        c = canvas.Canvas("temp.pdf", pagesize=portrait(A4))
        c.drawImage(fn, 0, 0,*portrait(A4))
        c.save()
        with open("temp.pdf","rb")as fp:
            pdf_reader = PdfFileReader(fp)
            result_pdf.append(pdf_reader)
    result_pdf.write(path + "\\" + "result.pdf")
    result_pdf.close()
    remove("temp.pdf")
    print("转换成功!")
else:
    exit(0)

Big Image
EOF
本文章采用CC BY-NC-SA 4.0进行许可。转载请注明出处!
上一篇

2020 年度书单


下一篇

JS TOC Generator