Python抓取公众号历史文章列表,轻松实现!
优采云 发布时间: 2023-04-10 12:18作为一名自媒体人,我们都知道公众号的重要性。今天,我将向大家介绍如何使用 Python 程序获取公众号历史文章列表,让你的工作更加高效。
一、前言
在之前,我们可能需要手动地去获取公众号的历史文章列表。这样做不仅费时费力,而且还容易出错。但是使用 Python 程序就可以轻松地解决这个问题。
二、如何获取公众号历史文章列表
1.获取 cookies 和b667774d7d300216d283b2378e4153bf
首先,我们需要从浏览器中获取 cookies 和b667774d7d300216d283b2378e4153bf。为了方便起见,我们可以使用 Chrome 浏览器,并安装 EditThisCookie 插件来完成此步骤。
2.构造请求头和请求参数
接下来,我们需要构造请求头和请求参数。请注意,在构造请求参数时需要使用到上一步中获取到的 token 值。
3.发送请求并解析返回结果
最后,我们需要发送请求并解析返回结果。在这里,我们可以使用 requests 库来发送 HTTP 请求,并使用 BeautifulSoup 库来解析 HTML 结果。
三、完整代码实现
下面是完整的 Python 代码实现:
python
import requests
from bs4 import BeautifulSoup
def get_articles_list(account, cookies,b667774d7d300216d283b2378e4153bf):
url ='https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz={}&scene=124#wechat_redirect'.format(account)
headers ={
'User-Agent':'Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Cookie': cookies
}
params ={
'action':'getmsg',
'__biz': account,
'f':'json',
'offset':0,
'count': 10,
'is_ok':1,
'scene': 124,
'uin':'777',
'key':b667774d7d300216d283b2378e4153bf,
'pass_ticket':'',
'wxtoken':'',
'appmsg_token':b667774d7d300216d283b2378e4153bf,
'x5': 0
}
res = requests.get(url, headers=headers, params=params)
soup = BeautifulSoup(res.text,'html.parser')
articles_list =[]
for item in soup.select('script'):
if not item.string:
continue
if item.string.strip().startswith('var msgList'):
data = item.string.strip()[16:-1]
articles = json.loads(8d777f385d3dfec8815d20f7496026dc)['list']
for article in articles:
articles_list.append({
'title': article['app_msg_ext_info']['title'],
'url': article['app_msg_ext_info']['content_url']
})
return articles_list
四、总结
在本文中,我们介绍了如何使用 Python 程序获取公众号历史文章列表。通过这种方法,我们可以轻松地获取到公众号的所有文章,并将其用于进一步的分析和处理。如果你有更好的方法,欢迎在评论区分享。