获取公众号的fakeid
模拟公众号登录,获取
cookie和token
import time
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from yaml import safe_dump
service = Service(ChromeDriverManager().install())
options = webdriver.ChromeOptions()
options.add_experimental_option(
"excludeSwitches", ["enable-automation", "enable-logging"]
)
cookieTokens = []
while True:
driver = webdriver.Chrome(service=service, options=options)
driver.get("https://mp.weixin.qq.com/")
time.sleep(20)
driver.get("https://mp.weixin.qq.com/")
cookie_items = driver.get_cookies()
current_url = driver.current_url
token = current_url.split("token=")[-1]
cookie_string = ""
for cookie_item in cookie_items:
cookie_string += f"{cookie_item['name']}={cookie_item['value']};"
cookieTokens.append({"cookie": cookie_string, "token": str(token)})
driver.quit()
resp = input("是否需要新的公众号配置信息: (y/n):")
if resp.strip().lower() == "n":
break
config_path = Path(__file__).resolve().parent.parent.joinpath("config", "config.txt")
with open(config_path, "w", encoding="utf-8") as f:
f.write(str(cookieTokens))通过
cookie和token去查询相应公众号的fakeid
import requests
headers = {
cookie: cookie,
}
keyword="剧在"
url = f"https://mp.weixin.qq.com/cgi-bin/searchbiz?action=search_biz&query={}&token={token}"
response = requests.get(url, headers=headers)
result = response.json()从
result中可以解析到剧在所对应的fakeid就是Mzk2ODA0MDQ4Ng==
{
"fakeid": "Mzk2ODA0MDQ4Ng==",
"nickname": "剧焦",
"alias": "bkjuchang",
"round_head_img": "http://mmbiz.qpic.cn/sz_mmbiz_png/CicTQBqpbW6O53YtzhM5VXFo39LnMZsg3ODXzXUoqre953zHj4qlAYzOv9N6iaxhKREm8Fia6sBn8fe38D9wfwHIQ/0?wx_fmt=png",
"service_type": 1,
"signature": "从台前到幕后,让我们对戏剧了解多一点。",
"verify_status": 0
}