失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微信公众号文章采集工具 可采集文章文字内容信息及图片

微信公众号文章采集工具 可采集文章文字内容信息及图片

时间:2022-01-30 00:25:30

相关推荐

微信公众号文章采集工具 可采集文章文字内容信息及图片

微信公众号文章采集工具,可采集文章文字内容信息及图片

只能单篇采集,想要批量的不用看了!

可作为微信公众号文章图片采集助手使用!

需知:

1.exe程序运行环境为win7 64位操作系统!

2.部分文章图片采集存在BUG,望见谅!

3.工具为python编写,技术渣,只能到这里了!

微信公众号文章采集工具说明:

1.打开weixincj.exe文件

2.输入需要采集的微信公众号文章链接地址

3.回车等待程序运行

4.采集完毕5s后程序自动退出

采集过程中会自动生成目录 weixin

采集完毕,采集内容存放于weixin目录下的微信公众号文章标题目录

内容为图片及txt文档

下载地址

百度云:

链接: /s/1pCqptL6QwnP2eUeyAABnYA 提取码: sxca

需知:

exe程序运行环境为win7 64位操作系统!

提示:

不一定保证格式内容及图片完整性!

附上主要python源码:

#微信文章页采集# -*- coding: UTF-8 -*-import requestsimport re,time,osfrom bs4 import BeautifulSoupfrom baocun import bctp,bcwbfrom fake_useragent import UserAgentdef ua():ua = UserAgent()headers = {"User-Agent": ua.random}return headersdef get_content(url):headers=ua()respnese=requests.get(url,headers=headers).textsoup=BeautifulSoup(respnese,'lxml')#获取标题h2=soup.find('h2',class_="rich_media_title").get_text()h2=h2.replace('\n','')h2 = h2.replace(' ', '')h2 = re.sub(r'[\|\/\<\>\:\*\?\\\"]', "_", h2) # 剔除不合法字符print(f'微信公众号文章标题:{h2}')os.makedirs(f'weixin/{h2}/',exist_ok=True)lj =f'weixin/{h2}/'ljj=f'weixin/{h2}/{h2}.txt'author=soup.find('div',class_="rich_media_meta_list").find('a',id="js_name").get_text()author = author.replace('\n', '')author = author.replace(' ', '')author=f'来源:{author}'print(author)i=1text=''ps=soup.find('div',class_="rich_media_content").find_all('p')for p in ps:if "img" in str(p):try:img_url=p.find('img')['data-src']print(img_url)if "jpeg" == img_url[-4:]:img_name=f'{i}.{img_url[-4:]}'else:img_name = f'{i}.{img_url[-3:]}'bctp(lj, img_url, img_name)p_content=img_namei=i+1except Exception as e:print(f"获取图片数据失败,错误代码:{e}")passelse:p_content=p.get_text()text = '%s%s%s' % (text, '\n',p_content)texts='%s%s%s%s%s'%(h2,'\n',author,'\n',text)print(texts)bcwb(ljj, texts)if __name__ == '__main__':url=input("请输入要采集的微信公众号文章链接:")print(f'爬虫启动中,请稍后......')get_content(url)print(f'采集完毕,程序5s后自动关闭!')time.sleep(5)

附改进 完整内容抓取,无格式

#微信公众号文章 完整版内容抓取def cs(url):headers = ua()respnese = requests.get(url, headers=headers).textsoup = BeautifulSoup(respnese, 'lxml')get_article(soup)def get_article(soup):# 获取标题h2 = soup.find('h2', class_="rich_media_title").get_text()h2 = h2.replace('\n', '')h2 = h2.replace(' ', '')h2 = re.sub(r'[\|\/\<\>\:\*\?\\\"]', "_", h2) # 剔除不合法字符print(f'微信公众号文章标题:{h2}')os.makedirs(f'weixin/{h2}/', exist_ok=True)lj = f'weixin/{h2}/'ljj = f'weixin/{h2}/{h2}.txt'author = soup.find('div', class_="rich_media_meta_list").find('a', id="js_name").get_text()author = author.replace('\n', '')author = author.replace(' ', '')author = f'来源:{author}'print(author)# 获取文字内容texts = soup.find('div', class_="rich_media_content").get_text()texts = '\n'.join(texts.split('。')) # 以句号 分割文本print(texts)texts = '%s%s%s%s%s' % (h2, '\n', author, '\n', texts)print(texts)bcwb(ljj, texts)# 获取所有图片i = 1imgs = soup.find('div', class_="rich_media_content").find_all('img')for img in imgs:img_url = img['data-src']print(img_url)if "jpeg" == img_url[-4:]:img_name = f'{i}.{img_url[-4:]}'else:img_name = f'{i}.{img_url[-3:]}'bctp(lj, img_url, img_name)i = i + 1

源码应用到的保存模块

#存储内容import requestsimport timefrom fake_useragent import UserAgentdef ua():ua = UserAgent()headers = {"User-Agent": ua.random}return headers#下载图片def bctp(lj,img_url,img_name):print("开始下载图片!")try:r = requests.get(img_url,headers=ua(),timeout=5)with open(f'{lj}/{img_name}', 'wb') as f:f.write(r.content)print(f'下载{img_name}图片成功!')time.sleep(1)except Exception as e:if "port=443): Read timed out" in str(e):time.sleep(2)try:r = requests.get(img_url, headers=ua(),timeout=5)with open(f'{lj}/{img_name}', 'wb') as f:f.write(r.content)print(f'下载{img_name}图片成功!')except Exception as e:print(f'下载{img_name}图片失败!')print(f'错误代码:{e}')with open(f'{lj}/spider.txt', 'a+', encoding='utf-8') as f:f.write(f'错误代码:{e}---下载 {img_url} 图片失败\n')else:print(f'下载{img_name}图片失败!')print(f'错误代码:{e}')with open(f'{lj}/spider.txt', 'a+', encoding='utf-8') as f:f.write(f'错误代码:{e}---下载 {img_url} 图片失败\n')#保存文本内容def bcwb(ljj,texts):print("开始保存文本")with open(ljj, 'w', encoding='utf-8') as f:f.write(texts)print(f'保存文本内容成功!')

如果觉得《微信公众号文章采集工具 可采集文章文字内容信息及图片》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。