python微信公众号文章列表信息获取

要整一个每天获取一批公众号发布的文章数据
大致流程如下
在这里插入图片描述
登录公众号后台—>创建图文—>点击超链接,即可选择公众号
在这里插入图片描述
浏览器F12可看到访问地址
https://mp.weixin.qq.com/cgi-bin/searchbiz?action=search_biz&begin=0&count=5&query=%E4%BA%BA%E6%B0%91%E6%97%A5%E6%8A%A5&token=1125413991&lang=zh_CN&f=json&ajax=1
里面的query参数就是你要查找的公众号名称
但是token长时间使用会失效,访问频率过高会被限制。
1.token获取
使用selenium通过chromedriver打开chrome浏览器每小时刷新页面保证能获取到最新的token和cookies信息保存到userMsg.txt文件中方便读取

#通过selenium打开Chrome浏览器用户扫码登录后页面定时刷新给token保活
#pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json

chrome_option = Options()

chrome_option.set_capability("goog:loggingPrefs", {
   
   "performance": "ALL"})

driver = webdriver.Chrome(options=chrome_option)
driver.get('https://mp.weixin.qq.com')#用户登录后自己扫码

locator = (By.ID, 'js_index_menu')#判断用户是否扫码加载出了公众号菜单列表

WebDriverWait(driver, 120, 0.5).until(EC.presence_of_element_located(locator))#等待120秒,120秒内未扫码失效
print('login')
wait = WebDriverWait(driver, 10)
element = driver.find_element(By.ID, "js_index_menu")

def getNewToken(driver):
    driver.refresh()#刷新页面
    print('----------------------------打印cookies信息----------------------------------------')
    # 获取当前页面的所有cookie
    cookies = driver.get_cookies()
    agent = driver.execute_script("return navigator.userAgent")
    token = ''
    cookieValue = ''
    for cookie in cookies:
        cookieValue = cookieValue+cookie[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值