思路
获取到微信公众号的文章之后,使用 BeautifulSoup 和正则表达式 解析出文章标题和包含正文的html.
获取到html之后,用正则表达式获取图片下载链接,下载成功后将微信的链接替换成本地图片链接。
运行效果
代码
import requests
from re import findall
from bs4 import BeautifulSoup
import time
import osweixin_title=""
weixin_time=""#获取微信公众号内容,保存标题和时间
def get_weixin_html(url):global weixin_time,weixin_titleres=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修改为srcfor 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=1flag=Truepic_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 = Falseprint('已下载第' + str(index) +'张图片.')count += 1time.sleep(1)except:count+=1time.sleep(1)if count>10:print("下载出错:",pic_url)return contentif __name__ == "__main__":#获取htmlinput_flag=Truewhile 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=Falsecontent=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)