2024-最新爬取公众号文章技术探讨和代码(2)

上次我们研究了使用公众号后台接口获取任意公众号历史文章的方式。查看点这里
今天我们研究第二种方案,使用微信PC端,访问任意公众号,然后获取历史文章的方式。
本次以“java-tech”公众号为例,手把手教你爬取公众号文章。

1、准备工作

  • 使用Charles抓包工具获取微信访问公众号和文章的接口。Charles下载地址:https://www.charlesproxy.com/download,我们也可以换Fiddler或者Wireshake。

  • 当访问下面公众号时就可以在Charles看到有接口交互。
    *

  • 下图中的mp.wixin.qq.com就是微信相关接口。我们从里面找到公众号相关API。

  • 一开始,我们没有在charles配置SSL 代理,所以应该看不到接口内容,都是 <unknown>

  • 安装charles证书,配置SSL代理,设置:mp.weixin.qq.com:443

  • 再次访问公众号,应该就能看到API交互内容了。我们在filter里面查看mp.weixin.qq.com的消息。
    注意: 如果微信PC提示访问链接不安全,请点击继续访问。

  • 定位查看历史消息的url:https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=,关键请求字段有__biz=标记某个公众号id,uin=当前用户id,key=包含过期时间的字段,offset是文章起始位置,count=返回数量,其他字段可以忽略。

  • 响应general_msg_list里面有作者,发布时间,文章地址,标题等。

2、技术实现

  1. 提取关键请求信息,比如biz, uin, key, offset, count等。
  2. 构造请求消息。
  3. 解析响应,提取一篇文章关键信息,包括日期、标题、题图、链接 。
  4. 判断是否存在下一页信息。
  5. 保存信息到excel或者csv。
  6. 进一步,可以把每篇文章保存成pdf。

3、代码献上

#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
import csv  
import json  
import time  
from datetime import date  
import requests  
  
__art_infos = []  
  
'''  
日期:2024年2月23日  
公众号ID:java-tech  
公众号:Java实用技术手册  
声明:本文仅供技术研究,请勿用于非法采集,后果自负。  
'''   
class ArtInfo(object):  
    """  
    文章信息:包括日期、标题、题图、链接  
    """  
  
    def __init__(self, pub_date, json_info):  
    self.pub_date = pub_date  
    self.title = json_info.get('title')  
    self.cover = json_info.get('cover')  
    self.content_url = json_info.get('content_url')  
  
  
def get_history(offset=0, count=10, continue_flag=True):  
    biz = '你的biz'  
    uin = '你的uin'  
    key = '你的key'  
    # pass_ticket = 'tS1eMbmDmdNLrZP5/55e6BrLg9TWDuGOFkOXYPIXM8N1SWkZbAEGALDh6kaMQO/GPFT1emxuf j0Rnxxlq iUw=='  
  
    url = f'https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz={biz}&f=json&offset={offset}&count={count}&is_ok=1&scene=124&uin={uin}&key={key}'  
    res = requests.get(url)  
    print(res.json())  
    js = res.json()  
    # print(js.get('general_msg_list').get('list'))  
    general_msg_list_str = js.get('general_msg_list')  
    msg_list_json = json.loads(general_msg_list_str)  
    art_list = msg_list_json.get('list')  
    for art in art_list:  
	    get_art_info(art)  
  
    # can_msg_continue 是用来判断是否可以继续获取文章的  
    if js.get('can_msg_continue') and continue_flag:  
	    offset = (offset+1) * count  
	    print(f'===继续获取下一页,offset={offset}')  
	    time.sleep(10)  # 防封  
	    get_history(offset, 10, False) # 测试一次就不继续  
  
    else:  
	    print('结束,没有更多文章。')  
  
  
def get_art_info(json_art_info):  
    comm_msg_info = json_art_info.get('comm_msg_info')  
  
    # 获取发文时间  
    datetime_ms = comm_msg_info.get('datetime')  
    pub_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(datetime_ms))  
    print('pub_datetime=', pub_datetime)  
    pub_date = str(date.fromtimestamp(datetime_ms))  
    info = json_art_info.get('app_msg_ext_info')  
    if info:  
	    # 头条消息  
	    art_info0 = ArtInfo(pub_date, info)  
	    # art_info0.parse(info)  
	    print('头条:', art_info0.__dict__)  
	    __art_infos.append(art_info0)  
	  
	    # 次消息  
	    multi_item_list = info.get('multi_app_msg_item_list')  
	    if multi_item_list:  
		    for item in multi_item_list:  
			    art_info = ArtInfo(pub_date, item)  
			    print('次头条:', art_info.__dict__)  
			    __art_infos.append(art_info)  
			    # break  
  
  
def save_art_info(art_infos):  
    with open('art_infos.csv', 'w', newline='') as f:  
	    writer = csv.writer(f)  
	    writer.writerow(['pub_date', 'title', 'cover', 'content_url'])  
	    for info in art_infos:  
		    writer.writerow([info.pub_date, info.title, info.cover, info.content_url])  
  
  
def main():  
    get_history()  
    save_art_info(__art_infos)  
  
  
if __name__ == '__main__':  
    main()

4、爬取结果

5、常见问题

  • 本文使用的微信PC版本为V2.6.4,如果你用的是最新的V3.x,你可能看不到/mp/profile_ext链接,虽然新版本链接被屏蔽了,但是这个接口依然可以调通。。。
  • 微信官网没有V2.6.4了,可以在本公众号 java-tech 回复 wx264 下载。
  • charles调测时需要打开电脑代理。
  • py调测报错:ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number,请关闭本地代理。

声明:本文仅供技术研究,请勿用于非法采集。

Python 爬取公众号图片通常涉及到网络请求、HTML解析文件存储等步骤。以下是基本的流程: 1. **安装所需库**: 首先需要安装 `requests` 库来进行 HTTP 请求,以及如 `beautifulsoup4` 或 `lxml` 进行 HTML 解析。 2. **发送请求**: 使用 `requests.get(url)` 获取公众号文章页面的 HTML 内容。记得检查网站是否允许爬虫访问,并遵守其robots.txt规则。 3. **定位图片元素**: 利用 BeautifulSoup 或其他解析库分析 HTML 结构,找到包含图片链接的元素。这通常是通过查找 `<img>` 标签并获取 `src` 属性来完成的。 ```python import requests from bs4 import BeautifulSoup url = 'https://mp.weixin.qq.com/s/<your_article_url>' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') images = soup.find_all('img', src=True) ``` 4. **保存图片**: 对于每个找到的图片链接,创建一个文件名(可能是基于URL生成),然后下载图片到本地。 ```python import os for img in images: img_url = img['src'] filename = os.path.join(os.getcwd(), os.path.basename(img_url)) with open(filename, 'wb') as f: response = requests.get(img_url, stream=True) for chunk in response.iter_content(1024): if chunk: f.write(chunk) ``` 5. **处理可能出现的问题**: - 有些网站可能会有防盗链机制,需要设置正确的User-AgentCookie。 - 分页爬取时,需要处理导航链接,递归或循环遍历。 - 注意版权法律问题,尊重网站规定,合理使用爬取信息。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值