爬取微信文章
Posted 阿布alone
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬取微信文章相关的知识,希望对你有一定的参考价值。
1.抓包
打开微信网页版
抓包:
根据接口数据构造请求,便能获取公众号文章了!
2.构造请求,获取数据
import requests import json import time def parse(__biz, uin, key, pass_ticket, appmsg_token="", offset="0"): """ 文章信息获取 """ url = ‘?txe_eliforp/pm/moc.qq.nixiew.pm//:sptth‘[::-1] 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")
数据:
以上是关于爬取微信文章的主要内容,如果未能解决你的问题,请参考以下文章