在做微信公众号爬取文章时,报错:当前环境异常,完成验证后即可继续访问。导致无法获取微信公众号文章。

微信公众号报错如下:

<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=Content-Type content="text/html;charset=utf-8">\n<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover">\n<meta name="color-scheme" content="light dark">\n<meta name="apple-mobile-web-app-capable" content="yes">\n<meta name="apple-mobile-web-app-status-bar-style" content="black">\n<meta name="format-detection" content="telephone=no">\n<link rel="shortcut icon" type="image/x-icon" href="https://res.wx.qq.com/a/wx_fed/assets/res/NTI4MWU5.ico">\n<link rel="mask-icon" href="https://res.wx.qq.com/a/wx_fed/assets/res/MjliNWVm.svg" color="#4C4C4C">\n<link rel="apple-touch-icon-precomposed" href="https://res.wx.qq.com/a/wx_fed/assets/res/OTE0YTAw.png">\n\n <title></title>\n <script>\n (() => {\n \n const ua = navigator.userAgent;\n const noMobile = !(/(iPhone|iPad|iPod|iOS)/i.test(ua) || /Windows\\sPhone/i.test(ua) || /(Android)/i.test(ua));\n setTimeout(() => {\n noMobile && document.title === \'\' && (document.title = \'微信公众平台\');\n }, 1000);\n })();\n </script>\n \n<link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/t/wx_fed/weui-source/res/2.5.14/weui.min.css">\n\n<link rel="stylesheet" href="https://res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/secitptpage/verify789d30.css" media="all">\n<script type="text/javascript">\n \n</script>\n\n </head>\n <body class="zh_CN " ontouchstart="">\n \n<div class="weui-msg">\n <div id="tips" style="display:none;" class="top_tips warning"></div>\n <div class="weui-msg__icon-area">\n <i class="weui-icon-info-circle weui-icon_msg"></i>\n </div>\n <div class="weui-msg__text-area pc-area">\n <h2 class="weui-msg__title">环境异常</h2>\n <p class="weui-msg__desc">当前环境异常,完成验证后即可继续访问。。。。。。。

这个是报错截图:

解决办法:

自定义一个请求头,我使用的是python代码,例如:

local_headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0'
}

原来我是直接使用response = requests.get(link_url1)发起请求,缺少请求头:User-Agent,
所以微信公众号认为我是爬虫,在爬取微信公众号文章,报错环境异常。
只需要改成如下代码即可解决这个:当前环境异常,完成验证后即可继续访问的报错,代码如下:

response = requests.get(link_url1, headers=local_headers)

下载成功截图:


结尾:

这个微信公众号文章下载工具,是我使用python pyqt5开发的,可以批量下载微信公众号文章,格式支持html pdf  docx,还支持下载文章封面图片。

有现成的下载工具,也可以提供源代码,有需要的朋友可以联系我微信:nanfangyoujialong。

联系我们