吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5392|回复: 26
收起左侧

[Python 原创] 可用于web部署的微信公众号下载工具

  [复制链接]
thisxx 发表于 2022-10-30 10:39
本帖最后由 thisxx 于 2022-11-1 17:45 编辑

如有违规请管理删帖。
自己用python写的微信公众号文章下载工具,部分代码参考CSDN。
无界面、主要是通过传参运行,以便用于web部署。内置了界面化输入链接的接口,可以自行把注释关掉。
功能:下载微信公众号文章到html,并将每篇文章打包为zip。
缺点:可以保存微信公众号内部上传图片,外链图片下载会出错,有懂得大佬可以指导一下。
==========================
3.png
==========================
部分核心代码
[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



wctohtmlandzip.zip (2.99 KB, 下载次数: 183)



免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
pentest686 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

dhwl9899 发表于 2022-10-30 10:52
谢谢大侠分享,应该是相当不错的,完后试试。
 楼主| thisxx 发表于 2022-10-31 09:46
风中飘雪 发表于 2022-10-30 20:31
能否批量现在公众号全部文章?

我这个主要是方便web部署用的,目前不支持批量。
你说的这个需求,论坛有人写了批量的。https://www.52pojie.cn/thread-1695991-1-1.html
xinyangtuina 发表于 2022-10-30 13:18
cfvgbhnj 发表于 2022-10-30 16:24
谢谢大侠分享,应该是相当不错的,
风中飘雪 发表于 2022-10-30 20:31
能否批量现在公众号全部文章?
XiaoBai.Q.Q 发表于 2022-10-30 22:20
这个可以有
ct268gh 发表于 2022-10-31 00:54
正好用来收藏有用的文章
neoyoung 发表于 2022-11-1 23:24
马上试试
好学 发表于 2022-11-3 12:17
下载试下,学习下 怎么获取列表等信息
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 09:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表