This repository has been archived on 2026-03-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
knowledge-base/data/zhihu/detail_contents_2026-02-27.json
T
starmen1265 623e4fab31
Knowledge Base Deploy / deploy-test (push) Successful in 1m40s
Knowledge Base Deploy / deploy-prod (push) Has been skipped
Add: 开源神器——分分钟爬取小红书、抖音、微博、知乎了 (知乎专栏)
2026-02-27 20:18:54 +08:00

22 lines
7.7 KiB
JSON
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
[
{
"content_id": "1894054073625461813",
"content_type": "article",
"content_text": "如何高效的爬取微信公众号\nnull\n睡到自然醒\n16 人赞同了该文章\n⚠️ 免责声明\n\n本文仅为个人学习与技术研究用途,所涉及的爬虫程序用于探索公开网页的数据结构及自动化处理方式。采集的数据未被用于任何商业用途,也不涉及敏感或受限信息的抓取。如有侵犯任何平台的相关规定或权益,请及时联系本人删除。 以下内容旨在分享爬虫开发中的经验与技术细节,希望对同样热爱技术的你有所启发。\n\n前提知识\n熟悉一门编程语言(如 Python / Java / Golang 等)\n了解基本网页结构(HTML / DOM)\n熟悉 Selenium 基本操作\n️ 微信公众号文章爬取说明\n❌ 过去的方式:搜狗搜索接口(已失效)\n\n早期,我们可以通过 搜狗搜索接口 获取公众号文章数据,无需登录验证,非常方便。\n但随着平台策略变化,该方式已无法继续使用。\n\n✅ 当前方法:微信公众号后台接口爬取\n\n\n\n\n\n\n\n我们通过 微信公众号后台文章管理接口 来获取文章列表,实现爬取逻辑。\n\n该接口返回的是你关注或搜索到的公众号所发布的文章列表,其 URL 中包含两个关键参数:\n\nfakeid:表示公众号的唯一标识符。\ntoken:登录后平台发放的访问令牌。\n\n此外,接口请求还必须携带有效的登录 Cookie。\n\n⚠️ 注意事项:\ntoken 和 cookie 都具有时效性(大约有效期为 2~3 天)。\n一旦过期,爬虫程序将无法获取数据,必须重新登录。\n解决方案:自动化登录\n\n为了确保爬虫长期稳定运行,我们采用 Selenium 自动扫码登录 的方式,获取登录后的 token 与 cookie。\n\n实现逻辑如下:\n启动浏览器,访问微信公众号后台。\n扫码登录后,从浏览器请求中提取 token 和 cookie。\n将有效信息保存到数据库中。\n爬虫启动时从数据库读取公众号列表及对应的文章接口地址。\n检查 token / cookie 是否过期,若失效,则自动触发重新扫码流程,刷新数据。\n\n\n\n\n我讲讲我目前的处理方式,首选数据库存储我要爬哪些公众号,将公众号名称存储下来,然后记录上图中的url,然后将token和cookie存储下来。遍历所有的公众号调用接口即可实现,如果token过期就采用自动化的方式更新token和cookie即可!伪代码如下,可以根据自己的业务进行更改。只提供思路。\n\noptions = webdriver.ChromeOptions()\ndriver = webdriver.Chrome(options=options)\ndriver.get(\"https://mp.weixin.qq.com/\")\ntry:\n WebDriverWait(driver, 10).until(\n EC.visibility_of_element_located((By.CLASS_NAME, \"login__type__container__scan__qrcode\"))\n )\n while True:\n current_url = driver.current_url\n if \"token\" in current_url:\n print(\"登录成功,检测到token 准备进行跳转\")\n parsed_url = urlparse.urlparse(current_url)\n token = urlparse.parse_qs(parsed_url.query).get(\"token\", [\"\"])[0]\n timestamp = int(time.time() * 1000)\n new_url = (\n f\"https://mp.weixin.qq.com/cgi-bin/appmsg?\"\n f\"t=media/appmsg_edit_v2&action=edit&isNew=1&type=77&createType=0\"\n f\"&token={token}&lang=zh_CN&timestamp={timestamp}\"\n )\n\n driver.get(new_url)\n print(f\"跳转到新页面: {new_url}\")\n WebDriverWait(driver, 10).until(\n EC.element_to_be_clickable((By.ID, \"js_editor_insertlink\"))\n ).click()\n print(\"已自动点击超链接\")\n\n # <button type=\"button\" class=\"weui-desktop-btn weui-desktop-btn_default\">选择其他公众号</button>\n WebDriverWait(driver, 10).until(\n EC.element_to_be_clickable((By.CSS_SELECTOR, \".weui-desktop-btn.weui-desktop-btn_default\"))\n ).click()\n print(\"已自动点击选择其他公众号\")\n input_box = WebDriverWait(driver, 50).until(\n EC.visibility_of_element_located(\n (By.XPATH,\n \"//input[@class='weui-desktop-form__input' and @placeholder='输入文章来源的账号名称或微信号,回车进行搜索']\"))\n )\n print(\"已自动键入北京\")\n input_box.send_keys(\"北京\")\n\n search_button = WebDriverWait(driver, 10).until(\n EC.presence_of_element_located((By.CSS_SELECTOR, \".weui-desktop-icon.weui-desktop-icon__search\"))\n )\n driver.execute_script(\"arguments[0].click();\", search_button)\n print(\"已自动点击搜索\")\n\n WebDriverWait(driver, 10).until(\n lambda driver: any(\"searchbiz\" in request.url for request in driver.requests)\n )\n\n for request in driver.requests:\n if \"searchbiz\" in request.url:\n\n cookie = request.headers.get(\"Cookie\")\n cookie = [cookie.strip() for cookie in cookie.split(';') if cookie]\n cookie_dict = {item.split('=')[0]: item.split('=')[1] for item in cookie}\n formatted_cookies = ',\\n'.join([f'\"{key}\": \"{value}\"' for key, value in cookie_dict.items()])\n\n new_cookie = \"{\" + formatted_cookies + \"}\"\n conn = db_factory.get_conn()\n cur = conn.cursor()\n cur.execute(update_property_sql, (new_cookie, 'vx_cookie'))\n cur.execute(update_property_sql, (token, 'vx_token'))\n cur.execute(update_property_sql, (1, 'vx_available'))\n conn.commit()\n wx_spy_manager = WxSpyManager()\n wx_spy_manager.get_more_data()\n break\n print(\"自动化爬虫完毕\")\n break\n\n time.sleep(1) # 每秒检查一次\n✅ 小结\n\n本方案基于微信公众号后台接口,实现了较为稳定的自动化文章爬取功能。通过 Selenium 自动化登录,可以解决传统接口过期失效的问题,为数据采集提供便利。\n\n后续文章将介绍爬虫核心模块的设计、异常处理、并发优化等更多实践内容。欢迎持续关注!\n\n发布于 2025-04-11 16:08・重庆\npython爬虫\n微信公众号运营\n阿里云百炼Coding Plan多模型,邀请至高得3000元!\n每邀请1位阿里云百炼新客购买通用AI节省计划、Coding Plan即可得30元,受邀人... \n查看详情\n阿里云\n的广告\n​\n赞同 16​\n​\n1 条评论\n​\n分享\n​\n喜欢\n​\n收藏\n​\n申请转载\n​\n​",
"content_url": "https://zhuanlan.zhihu.com/p/1894054073625461813",
"question_id": "",
"title": "如何高效的爬取微信公众号",
"desc": "⚠️ 免责声明本文仅为个人学习与技术研究用途,所涉及的爬虫程序用于探索公开网页的数据结构及自动化处理方式。采集的数据未被用于任何商业用途,也不涉及敏感或受限信息的抓取。如有侵犯任何平台的相关规定或权益,请及时联系本人删除。 以下内容旨在…",
"created_time": 1744358937,
"updated_time": 1744358937,
"voteup_count": 0,
"comment_count": 0,
"source_keyword": "",
"user_id": "8aa5637cd525b975f4fa69be867583c2",
"user_link": "https://www.zhihu.com/people/None",
"user_nickname": "null",
"user_avatar": null,
"user_url_token": null,
"last_modify_ts": 1772184724253
}
]