Python批量抓取公众号文字,轻松提高信息获取效率!

优采云 发布时间: 2023-04-06 13:15

  公众号文章是获取信息的重要渠道,但是手动复制粘贴操作耗费时间且效率低下。本文将介绍如何使用Python批量抓取公众号文字,提高信息获取效率。

  一、准备工作

  在开始之前需要安装Python环境和以下库:requests、lxml、beautifulsoup4。接着,我们需要获取公众号的tokenfakeid

  二、获取token和fakeid

  

  我们可以通过微信公众平台接口获取token和fakeid。首先,登录微信公众平台,在开发->基本配置中找到开发者ID开发者密码。接着使用以下代码获取token:

  python

import requests

app_id ="your_app_id"

app_secret ="your_app_secret"

url =f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={app_id}&secret={app_secret}"

response = requests.get(url)

access_token = response.json()["access_token"]

  然后,我们可以使用以下代码获取fakeid:

  

  python

url =f"https://mp.weixin.qq.com/cgi-bin/searchbiz?action=search_biz&token={access_token}&lang=zh_CN&f=json&query=公众号名称"

cookies ={"Cookie":"your_cookie"}

response = requests.get(url, cookies=cookies)

fakeid = response.json()["list"][0]["fakeid"]

  三、抓取文章

  有了token和fakeid,我们就可以使用以下代码抓取公众号文章:

  

  python

url ="https://mp.weixin.qq.com/cgi-bin/appmsg"

params ={

"action":"list_ex",

"query":"",

"fakeid": fakeid,

"type":9,

"token": access_token,

"lang":"zh_CN",

"f":"json",

"ajax":1,

}

cookies ={"Cookie":"your_cookie"}

articles =[]

has_next_page = True

while has_next_page:

response = requests.get(url, params=params, cookies=cookies)

result = response.json()

articles.extend(result["app_msg_list"])

has_next_page = result["has_next_page"]

if has_next_page:

params["offset"]= result["next_offset"]

for article in articles:

print(article["title"], article["link"])

  这段代码会抓取公众号所有文章的标题和链接。如果需要抓取文章的正文内容,可以在循环内部访问每篇文章的链接,再使用beautifulsoup4解析正文内容。

  四、总结

  通过Python批量抓取公众号文字,可以提高信息获取效率。需要注意的是,在进行抓取前需要获取token和fakeid,并且需要模拟登录以获取cookie。同时,为了防止被封IP,建议添加代理池和随机延时等策略。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线