Python下载微信公众号文章和图片保存至本地

1 篇文章 0 订阅
1 篇文章 0 订阅

思路

获取到微信公众号的文章之后,使用 BeautifulSoup 和正则表达式 解析出文章标题和包含正文的html.

获取到html之后,用正则表达式获取图片下载链接,下载成功后将微信的链接替换成本地图片链接。

运行效果

在这里插入图片描述
在这里插入图片描述

代码

import requests
from re import findall
from bs4 import BeautifulSoup
import time
import os

weixin_title=""
weixin_time=""

#获取微信公众号内容,保存标题和时间
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]
    
    #获取正文html并修改
    content=soup.find(id='js_content')
    soup2=BeautifulSoup((str(content)),"html.parser")
    soup2.div['style']='visibility: visible;'
    html=str(soup2)
    pattern=r'http[s]?:\/\/[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='pic/'
    if not os.path.exists(pic_path):
        os.makedirs(pic_path)
        
    #使用正则表达式查找所有需要下载的图片链接
    pattern=r'http[s]?:\/\/[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_path + file_name)
                 
                 flag = False
                 print('已下载第' + str(index) +'张图片.')
                 count += 1
                 time.sleep(1)
                      
            except:
                 count+=1
                 time.sleep(1)
                 
        if count>10:
            print("下载出错:",pic_url)
    return content

if __name__ == "__main__":
    
    #获取html
    input_flag=True
    while input_flag:
       weixin_url=input('请输入微信文章链接后按Enter:')
       re=findall(r'http[s]?:\/\/mp.weixin.qq.com\/s\/[0-9a-zA-Z_]+',weixin_url) 
       if len(re)<=0:
            print("链接有误,请重新输入!")
       else:
           input_flag=False
     
    content=get_weixin_html(weixin_url)
    content=download_pic(content)
    #保存至本地
    with open(weixin_title+'.txt','w+',encoding="utf-8") as f:
        f.write(content) 
    with open(weixin_title+'.html','w+',encoding="utf-8") as f:
        f.write(content)  
        
    print()
    print("标题:《"+weixin_title+"》")
    print("发布时间:"+weixin_time)
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值