可用于web部署的微信公众号下载工具

如有违规请管理删帖。
自己用python写的微信公众号文章下载工具,部分代码参考CSDN。
无界面、主要是通过传参运行,以便用于web部署。内置了界面化输入链接的接口,可以自行把注释关掉。
功能:下载微信公众号文章到html,并将每篇文章打包为zip。
缺点:可以保存微信公众号内部上传图片,外链图片下载会出错,有懂得大佬可以指导一下。
部分核心代码
[mw_shl_code=python,true]#Created on Sun Oct 8 22:30:13 2022
#version 1.0.2
#@author: thisxx
#@实现将公众号文章自动下载为html并打包为zip
#@python调试时调用方法:python .\wechatPy2.py <公众号文章链接> <保存目录>
#@eg:python .\wechatPy2.py https://mp.weixin.qq.com/s/GSdK9it0N1As3H-pQ4mGdA C:\微信公众号下载\12
#编译后调用:wctohtmlandzip.exe <公众号文章链接> <保存目录>
#@编译的时候要用pyinstaller -F -c .\wctohtmlandzip.py,需要调用命令行的要用-c不能用-w。

#获取微信公众号内容,保存标题和时间
def get_weixin_html(url):

global weixin_time,weixin_title
res=requests.get(url)
soup=BeautifulSoup(res.text,"html.parser")

#获取标题
temp=soup.find('h1')
weixin_title=temp.string.strip()

#使用正则表达式获取时间
result=findall(r'[0-9]{4}-[0-9]{2}-[0-9]{2}.+:[0-9]{2}',res.text)
#weixin_time=result[0]
#weixin_time="2022-22-22"

#获取正文html并修改
content=soup.find(id='js_content')
soup2=BeautifulSoup((str(content)),"html.parser")
soup2.div['style']='visibility: visible;'
html=str(soup2)
pattern=r'http?:\/\/[a-z.A-Z_0-9\/\?=-_-]+'
result = findall(pattern, html)

#将data-src修改为src
for url in result:
    html=html.replace('data-src="'+url+'"','src="'+url+'"')
return html

#上传图片至服务器
def download_pic(content):

pic_path=htmlsavepath +'/'+ weixin_title + '/' + 'pic/'
if not os.path.exists(pic_path):
    os.makedirs(pic_path)
    
#使用正则表达式查找所有需要下载的图片链接
pattern=r'http?:\/\/[a-z.A-Z_0-9\/\?=-_-]+'
pic_list = findall(pattern, content)

for index, item in enumerate(pic_list,1):
    count=1
    flag=True
    pic_url=str(item)
    
    while flag and count<=10:
        try:
             data=requests.get(pic_url);

             if pic_url.find('png')>0:
                 file_name = str(index)+'.png'
                 
             elif pic_url.find('gif')>0:
                 file_name=str(index)+'.gif'
                 
             else:
                 file_name=str(index)+'.jpg'

             with open(pic_path + file_name,"wb") as f:
                 f.write(data.content)
                 
             #将图片链接替换为本地链接
             content = content.replace(pic_url,'pic/' + file_name)
             
             flag = False
             #print('已下载第' + str(index) +'张图片.')
             print('...')
             count += 1
             #time.sleep(1)#下载每张图片后睡眠
                 
        except:
             count+=1
             time.sleep(1)
             
    if count>10:
        print("下载出错:",pic_url)
return content
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值