import sys
import requests
import json
import time
from pymongo import MongoClient


url = "http://mp.weixin.qq.com/mp/profile_ext"

# Mongo配置
conn = MongoClient("127.0.0.1", 27017)
db = conn.wx  # 连接wx数据库，没有则自动创建
mongo_wx = db.article  # 使用article集合，没有则自动创建
mongo_wx.remove()

data = []


def get_wx_article(argv, cfg, index=0, count=10):
    offset = (index + 1) * count
    params = {
        "__biz": cfg.biz,
        "uin": cfg.uin,
        "key": cfg.key,
        "offset": offset,
        "count": count,
        "action": "getmsg",
        "f": "json",
    }

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
    }

    response = requests.get(url=url, params=params, headers=headers)
    resp_json = response.json()
    if resp_json.get("errmsg") == "ok":
        resp_json = response.json()
        # 是否还有分页数据， 用于判断return的值
        can_msg_continue = resp_json["can_msg_continue"]
        # 当前分页文章数
        msg_count = resp_json["msg_count"]
        general_msg_list = json.loads(resp_json["general_msg_list"])
        # print(general_msg_list)
        print("\n\n\n\n\n\n\n\n\n")
        list = general_msg_list.get("list")
        # print(list)
        print("\n\n\n\n\n\n\n\n\n")
        print(list, "**************\n")
        for i in list:
            app_msg_ext_info = i["app_msg_ext_info"]
            # 标题
            title = app_msg_ext_info["title"]
            # 文章地址
            content_url = app_msg_ext_info["content_url"]
            # 封面图
            cover = app_msg_ext_info["cover"]

            # 发布时间
            datetime = i["comm_msg_info"]["datetime"]
            datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(datetime))
            # 使用数据库
            if argv[1] == "1":
                mongo_wx.insert(
                    {
                        "title": title,
                        "content_url": content_url,
                        "cover": cover,
                        "datetime": datetime,
                    }
                )
            # 使用json文件
            else:
                data.append(
                    {
                        "title": title,
                        "content_url": content_url,
                        "cover": cover,
                        "datetime": datetime,
                    }
                )
        if can_msg_continue == 1:
            return True
        return False
    else:
        print("获取文章异常...")
        return False


if __name__ == "__main__":

    import cfg

    index = 0
    while 1:
        print(f"开始抓取公众号第{index + 1} 页文章.")
        flag = get_wx_article(sys.argv, cfg, index=index)

        # 使用数据库输入1
        if sys.argv[1] != 1:
            with open("data.json", "w") as fw:
                json.dump(data, fw)
        # 防止和谐，暂停8秒
        time.sleep(8)
        index += 1
        if not flag:
            print("公众号文章已全部抓取完毕，退出程序.")
            break

        print(f"..........准备抓取公众号第{index + 1} 页文章.")


71c513839bb0a83f7cdcc743388bfc29ad039fc8
<mask token>


def get_wx_article(argv, cfg, index=0, count=10):
    offset = (index + 1) * count
    params = {'__biz': cfg.biz, 'uin': cfg.uin, 'key': cfg.key, 'offset':
        offset, 'count': count, 'action': 'getmsg', 'f': 'json'}
    headers = {'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
        }
    response = requests.get(url=url, params=params, headers=headers)
    resp_json = response.json()
    if resp_json.get('errmsg') == 'ok':
        resp_json = response.json()
        can_msg_continue = resp_json['can_msg_continue']
        msg_count = resp_json['msg_count']
        general_msg_list = json.loads(resp_json['general_msg_list'])
        print('\n\n\n\n\n\n\n\n\n')
        list = general_msg_list.get('list')
        print('\n\n\n\n\n\n\n\n\n')
        print(list, '**************\n')
        for i in list:
            app_msg_ext_info = i['app_msg_ext_info']
            title = app_msg_ext_info['title']
            content_url = app_msg_ext_info['content_url']
            cover = app_msg_ext_info['cover']
            datetime = i['comm_msg_info']['datetime']
            datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                datetime))
            if argv[1] == '1':
                mongo_wx.insert({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
            else:
                data.append({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
        if can_msg_continue == 1:
            return True
        return False
    else:
        print('获取文章异常...')
        return False


<mask token>

<mask token>
mongo_wx.remove()
<mask token>


def get_wx_article(argv, cfg, index=0, count=10):
    offset = (index + 1) * count
    params = {'__biz': cfg.biz, 'uin': cfg.uin, 'key': cfg.key, 'offset':
        offset, 'count': count, 'action': 'getmsg', 'f': 'json'}
    headers = {'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
        }
    response = requests.get(url=url, params=params, headers=headers)
    resp_json = response.json()
    if resp_json.get('errmsg') == 'ok':
        resp_json = response.json()
        can_msg_continue = resp_json['can_msg_continue']
        msg_count = resp_json['msg_count']
        general_msg_list = json.loads(resp_json['general_msg_list'])
        print('\n\n\n\n\n\n\n\n\n')
        list = general_msg_list.get('list')
        print('\n\n\n\n\n\n\n\n\n')
        print(list, '**************\n')
        for i in list:
            app_msg_ext_info = i['app_msg_ext_info']
            title = app_msg_ext_info['title']
            content_url = app_msg_ext_info['content_url']
            cover = app_msg_ext_info['cover']
            datetime = i['comm_msg_info']['datetime']
            datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                datetime))
            if argv[1] == '1':
                mongo_wx.insert({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
            else:
                data.append({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
        if can_msg_continue == 1:
            return True
        return False
    else:
        print('获取文章异常...')
        return False


if __name__ == '__main__':
    import cfg
    index = 0
    while 1:
        print(f'开始抓取公众号第{index + 1} 页文章.')
        flag = get_wx_article(sys.argv, cfg, index=index)
        if sys.argv[1] != 1:
            with open('data.json', 'w') as fw:
                json.dump(data, fw)
        time.sleep(8)
        index += 1
        if not flag:
            print('公众号文章已全部抓取完毕，退出程序.')
            break
        print(f'..........准备抓取公众号第{index + 1} 页文章.')

<mask token>
url = 'http://mp.weixin.qq.com/mp/profile_ext'
conn = MongoClient('127.0.0.1', 27017)
db = conn.wx
mongo_wx = db.article
mongo_wx.remove()
data = []


def get_wx_article(argv, cfg, index=0, count=10):
    offset = (index + 1) * count
    params = {'__biz': cfg.biz, 'uin': cfg.uin, 'key': cfg.key, 'offset':
        offset, 'count': count, 'action': 'getmsg', 'f': 'json'}
    headers = {'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
        }
    response = requests.get(url=url, params=params, headers=headers)
    resp_json = response.json()
    if resp_json.get('errmsg') == 'ok':
        resp_json = response.json()
        can_msg_continue = resp_json['can_msg_continue']
        msg_count = resp_json['msg_count']
        general_msg_list = json.loads(resp_json['general_msg_list'])
        print('\n\n\n\n\n\n\n\n\n')
        list = general_msg_list.get('list')
        print('\n\n\n\n\n\n\n\n\n')
        print(list, '**************\n')
        for i in list:
            app_msg_ext_info = i['app_msg_ext_info']
            title = app_msg_ext_info['title']
            content_url = app_msg_ext_info['content_url']
            cover = app_msg_ext_info['cover']
            datetime = i['comm_msg_info']['datetime']
            datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                datetime))
            if argv[1] == '1':
                mongo_wx.insert({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
            else:
                data.append({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
        if can_msg_continue == 1:
            return True
        return False
    else:
        print('获取文章异常...')
        return False


if __name__ == '__main__':
    import cfg
    index = 0
    while 1:
        print(f'开始抓取公众号第{index + 1} 页文章.')
        flag = get_wx_article(sys.argv, cfg, index=index)
        if sys.argv[1] != 1:
            with open('data.json', 'w') as fw:
                json.dump(data, fw)
        time.sleep(8)
        index += 1
        if not flag:
            print('公众号文章已全部抓取完毕，退出程序.')
            break
        print(f'..........准备抓取公众号第{index + 1} 页文章.')

import sys
import requests
import json
import time
from pymongo import MongoClient
url = 'http://mp.weixin.qq.com/mp/profile_ext'
conn = MongoClient('127.0.0.1', 27017)
db = conn.wx
mongo_wx = db.article
mongo_wx.remove()
data = []


def get_wx_article(argv, cfg, index=0, count=10):
    offset = (index + 1) * count
    params = {'__biz': cfg.biz, 'uin': cfg.uin, 'key': cfg.key, 'offset':
        offset, 'count': count, 'action': 'getmsg', 'f': 'json'}
    headers = {'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
        }
    response = requests.get(url=url, params=params, headers=headers)
    resp_json = response.json()
    if resp_json.get('errmsg') == 'ok':
        resp_json = response.json()
        can_msg_continue = resp_json['can_msg_continue']
        msg_count = resp_json['msg_count']
        general_msg_list = json.loads(resp_json['general_msg_list'])
        print('\n\n\n\n\n\n\n\n\n')
        list = general_msg_list.get('list')
        print('\n\n\n\n\n\n\n\n\n')
        print(list, '**************\n')
        for i in list:
            app_msg_ext_info = i['app_msg_ext_info']
            title = app_msg_ext_info['title']
            content_url = app_msg_ext_info['content_url']
            cover = app_msg_ext_info['cover']
            datetime = i['comm_msg_info']['datetime']
            datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                datetime))
            if argv[1] == '1':
                mongo_wx.insert({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
            else:
                data.append({'title': title, 'content_url': content_url,
                    'cover': cover, 'datetime': datetime})
        if can_msg_continue == 1:
            return True
        return False
    else:
        print('获取文章异常...')
        return False


if __name__ == '__main__':
    import cfg
    index = 0
    while 1:
        print(f'开始抓取公众号第{index + 1} 页文章.')
        flag = get_wx_article(sys.argv, cfg, index=index)
        if sys.argv[1] != 1:
            with open('data.json', 'w') as fw:
                json.dump(data, fw)
        time.sleep(8)
        index += 1
        if not flag:
            print('公众号文章已全部抓取完毕，退出程序.')
            break
        print(f'..........准备抓取公众号第{index + 1} 页文章.')

import sys
import requests
import json
import time
from pymongo import MongoClient


url = "http://mp.weixin.qq.com/mp/profile_ext"

# Mongo配置
conn = MongoClient("127.0.0.1", 27017)
db = conn.wx  # 连接wx数据库，没有则自动创建
mongo_wx = db.article  # 使用article集合，没有则自动创建
mongo_wx.remove()

data = []


def get_wx_article(argv, cfg, index=0, count=10):
    offset = (index + 1) * count
    params = {
        "__biz": cfg.biz,
        "uin": cfg.uin,
        "key": cfg.key,
        "offset": offset,
        "count": count,
        "action": "getmsg",
        "f": "json",
    }

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
    }

    response = requests.get(url=url, params=params, headers=headers)
    resp_json = response.json()
    if resp_json.get("errmsg") == "ok":
        resp_json = response.json()
        # 是否还有分页数据， 用于判断return的值
        can_msg_continue = resp_json["can_msg_continue"]
        # 当前分页文章数
        msg_count = resp_json["msg_count"]
        general_msg_list = json.loads(resp_json["general_msg_list"])
        # print(general_msg_list)
        print("\n\n\n\n\n\n\n\n\n")
        list = general_msg_list.get("list")
        # print(list)
        print("\n\n\n\n\n\n\n\n\n")
        print(list, "**************\n")
        for i in list:
            app_msg_ext_info = i["app_msg_ext_info"]
            # 标题
            title = app_msg_ext_info["title"]
            # 文章地址
            content_url = app_msg_ext_info["content_url"]
            # 封面图
            cover = app_msg_ext_info["cover"]

            # 发布时间
            datetime = i["comm_msg_info"]["datetime"]
            datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(datetime))
            # 使用数据库
            if argv[1] == "1":
                mongo_wx.insert(
                    {
                        "title": title,
                        "content_url": content_url,
                        "cover": cover,
                        "datetime": datetime,
                    }
                )
            # 使用json文件
            else:
                data.append(
                    {
                        "title": title,
                        "content_url": content_url,
                        "cover": cover,
                        "datetime": datetime,
                    }
                )
        if can_msg_continue == 1:
            return True
        return False
    else:
        print("获取文章异常...")
        return False


if __name__ == "__main__":

    import cfg

    index = 0
    while 1:
        print(f"开始抓取公众号第{index + 1} 页文章.")
        flag = get_wx_article(sys.argv, cfg, index=index)

        # 使用数据库输入1
        if sys.argv[1] != 1:
            with open("data.json", "w") as fw:
                json.dump(data, fw)
        # 防止和谐，暂停8秒
        time.sleep(8)
        index += 1
        if not flag:
            print("公众号文章已全部抓取完毕，退出程序.")
            break

        print(f"..........准备抓取公众号第{index + 1} 页文章.")

