上山打老虎 发表于 2021-6-24 10:00:48

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

微信公众号文章采集工具,可采集文章文字内容信息及图片  只能单篇采集,想要批量的不用看了!
可作为微信公众号文章图片采集助手使用!
  需知:
1.exe程序运行环境为win7 64位操作系统!
2.部分文章图片采集存在BUG,望见谅!
3.工具为python编写,技术渣,只能到这里了!

  微信公众号文章采集工具说明:
1.打开weixincj.exe文件
2.输入需要采集的微信公众号文章链接地址
3.回车等待程序运行
4.采集完毕5s后程序自动退出

  采集过程中会自动生成目录 weixin
采集完毕,采集内容存放于weixin目录下的微信公众号文章标题目录
内容为图片及txt文档

  下载地址
百度云:
链接: https://pan.baidu.com/s/1pCqptL6QwnP2eUeyAABnYA 提取码: sxca
  需知:
exe程序运行环境为win7 64位操作系统!
  提示:
不一定保证格式内容及图片完整性!
  附上主要python源码:
#微信文章页采集
# -- coding: UTF-8 --
import requests
import re,time,os
from bs4 import BeautifulSoup
from baocun import bctp,bcwb
from fake_useragent import UserAgent

def ua():
    ua = UserAgent()
    headers = {"User-Agent": ua.random}
return headers
def get_content(url):
    headers=ua()
respnese=requests.get(url,headers=headers).text
    soup=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=1
    text=''
    ps=soup.find('div',class_="rich_media_content").find_all('p')
    for p in ps:

      <span class="token keyword">if</span> <span class="token string">"img"</span> <span class="token keyword">in</span> <span class="token builtin">str</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">try</span><span class="token punctuation">:</span>
                img_url<span class="token operator">=</span>p<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'img'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'data-src'</span><span class="token punctuation">]</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span>img_url<span class="token punctuation">)</span>
                <span class="token keyword">if</span> <span class="token string">"jpeg"</span> <span class="token operator">==</span> img_url<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                  img_name<span class="token operator">=</span>f<span class="token string">'{i}.{img_url[-4:]}'</span>
                <span class="token keyword">else</span><span class="token punctuation">:</span>
                  img_name <span class="token operator">=</span> f<span class="token string">'{i}.{img_url[-3:]}'</span>
                bctp<span class="token punctuation">(</span>lj<span class="token punctuation">,</span> img_url<span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
                p_content<span class="token operator">=</span>img_name
                i<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span>
            <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">"获取图片数据失败,错误代码:{e}"</span><span class="token punctuation">)</span>
                <span class="token keyword">pass</span>
      <span class="token keyword">else</span><span class="token punctuation">:</span>
            p_content<span class="token operator">=</span>p<span class="token punctuation">.</span>get_text<span class="token punctuation">(</span><span class="token punctuation">)</span>
      text <span class="token operator">=</span> <span class="token string">'%s%s%s'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>text<span class="token punctuation">,</span> <span class="token string">'\n'</span><span class="token punctuation">,</span>p_content<span class="token punctuation">)</span>
    texts<span class="token operator">=</span><span class="token string">'%s%s%s%s%s'</span><span class="token operator">%</span><span class="token punctuation">(</span>h2<span class="token punctuation">,</span><span class="token string">'\n'</span><span class="token punctuation">,</span>author<span class="token punctuation">,</span><span class="token string">'\n'</span><span class="token punctuation">,</span>text<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>texts<span class="token punctuation">)</span>
    bcwb<span class="token punctuation">(</span>ljj<span class="token punctuation">,</span> texts<span class="token punctuation">)</span>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).text
    soup = 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 = 1
    imgs = soup.find('div', class_="rich_media_content").find_all('img')
    for img in imgs:

      img_url <span class="token operator">=</span> img<span class="token punctuation">[</span><span class="token string">'data-src'</span><span class="token punctuation">]</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span>img_url<span class="token punctuation">)</span>
      <span class="token keyword">if</span> <span class="token string">"jpeg"</span> <span class="token operator">==</span> img_url<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
            img_name <span class="token operator">=</span> f<span class="token string">'{i}.{img_url[-4:]}'</span>
      <span class="token keyword">else</span><span class="token punctuation">:</span>
            img_name <span class="token operator">=</span> f<span class="token string">'{i}.{img_url[-3:]}'</span>
      bctp<span class="token punctuation">(</span>lj<span class="token punctuation">,</span> img_url<span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
      i <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">1</span>  源码应用到的保存模块
#存储内容

import requests
import time
from fake_useragent import UserAgent

def ua():
    ua = UserAgent()
    headers = {"User-Agent": ua.random}
return headers
#下载图片
def bctp(lj,img_url,img_name):
    print("开始下载图片!")
    try:

      r <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>img_url<span class="token punctuation">,</span>headers<span class="token operator">=</span>ua<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>timeout<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
      <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>f<span class="token string">'{lj}/{img_name}'</span><span class="token punctuation">,</span> <span class="token string">'wb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
            f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>r<span class="token punctuation">.</span>content<span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">'下载{img_name}图片成功!'</span><span class="token punctuation">)</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
      <span class="token keyword">if</span> <span class="token string">"port=443): Read timed out"</span> <span class="token keyword">in</span> <span class="token builtin">str</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">:</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
            <span class="token keyword">try</span><span class="token punctuation">:</span>
                r <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>img_url<span class="token punctuation">,</span> headers<span class="token operator">=</span>ua<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>timeout<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
                <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>f<span class="token string">'{lj}/{img_name}'</span><span class="token punctuation">,</span> <span class="token string">'wb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
                  f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>r<span class="token punctuation">.</span>content<span class="token punctuation">)</span>
                  <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">'下载{img_name}图片成功!'</span><span class="token punctuation">)</span>
            <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">'下载{img_name}图片失败!'</span><span class="token punctuation">)</span>
                <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">'错误代码:{e}'</span><span class="token punctuation">)</span>
                <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>f<span class="token string">'{lj}/spider.txt'</span><span class="token punctuation">,</span> <span class="token string">'a+'</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
                  f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>f<span class="token string">'错误代码:{e}---下载 {img_url} 图片失败\n'</span><span class="token punctuation">)</span>
      <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">'下载{img_name}图片失败!'</span><span class="token punctuation">)</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">'错误代码:{e}'</span><span class="token punctuation">)</span>
            <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>f<span class="token string">'{lj}/spider.txt'</span><span class="token punctuation">,</span> <span class="token string">'a+'</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
                f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>f<span class="token string">'错误代码:{e}---下载 {img_url} 图片失败\n'</span><span class="token punctuation">)</span>#保存文本内容def bcwb(ljj,texts):    print("开始保存文本")    with open(ljj, 'w', encoding='utf-8') as f:
      f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>texts<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">'保存文本内容成功!'</span><span class="token punctuation">)</span>
页: [1]
查看完整版本: 微信公众号文章采集工具,可采集文章文字内容信息及图片