微信公众号数据抓取

1.抓取某个公众号全部的文件信息python

Charles +电脑版微信+pycharm+pythonjson

 

2. 分析微信

通过分析:每一个公众号文章列表页链接都是app

https://mp.weixin.qq.com/mp/profile_ext  开头 ,抓取时每一个公众号的只有几个参照不同函数

抓取:工具

 

 3. 代码ui

import requests
import json
import time

def parse(__biz, uin, key, pass_ticket, appmsg_token="", offset="0"):
    """
    文章信息获取
    """
    url = 'https://mp.weixin.qq.com/mp/profile_ext'
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.901.400 QQBrowser/9.0.2524.400",
    }
    params = {
        "action": "getmsg",
        "__biz": __biz,
        "f": "json",
        "offset": str(offset),
        "count": "10",
        "is_ok": "1",
        "scene": "124",
        "uin": uin,
        "key": key,
        "pass_ticket": pass_ticket,
        "wxtoken": "",
        "appmsg_token": appmsg_token,
        "x5": "0",
    }

    res = requests.get(url, headers=headers, params=params, timeout=3)
    data = json.loads(res.text)
    print(data)
    # 获取信息列表
    msg_list = eval(data.get("general_msg_list")).get("list", [])
    for i in msg_list:
        # 去除文字连接
        try:
            # 文章标题
            title = i["app_msg_ext_info"]["title"].replace(',', '')
            # 文章摘要
            digest = i["app_msg_ext_info"]["digest"].replace(',', '')
            # 文章连接
            url = i["app_msg_ext_info"]["content_url"].replace("\\", "").replace("http", "https")
            # 文章发布时间
            date = i["comm_msg_info"]["datetime"]
            print(title, digest, url, date)
            with open('article.csv', 'a') as f:
                f.write(title + ',' + digest + ',' + url + ',' + str(date) + '\n')
        except:
            pass
    # 判断是否可继续翻页 1-能够翻页  0-到底了
    if 1 == data.get("can_msg_continue", 0):
        time.sleep(3)
        parse(__biz, uin, key, pass_ticket, appmsg_token, data["next_offset"])
    else:
        print("爬取完毕")


if __name__ == '__main__':
    # 请求参数
    __biz = input('biz: ')
    uin = input('uin: ')
    key = input('key: ')
    pass_ticket = input('passtick: ')
    # 解析函数
    parse(__biz, uin, key, pass_ticket, appmsg_token="", offset="0")

经过抓取获取到不一样公众号的__biz, uin, key, pass_ticket的参数,就能够完成对该公众号的抓取。url

 注意: 程序运行前,用抓包工具获取须要的参数后,须要将charles 或filddle 抓包工具先关闭,而后再运行该程序,不然会报错。(这个程序须要电脑端微信登陆后,进行抓取,若是使用的是模拟器,模拟器上的uni和key 参数和电脑端不同,通常请求不能成功!)spa

相关文章
相关标签/搜索