
其实获取网页源码的方式有多种。控件如inet,webbrowser,代码量少些但不如用Microsoft.XMLHTTP对象轻便,且webbrowser基于ie,微软已经不支持了还要强制跳转到edge,所以隐患比较多。下面是代码:
dim url as string
url="https://www.baidu.com"
set xhp=createobject("microsoft.xmlhttp")
xhp.open "get",url,false
xhp.send
debug.print xhp.responseText
另外,看到有人说程序中使用Microsoft.XMLHTTP组件请求https时出现如下错误:

我没有遇到这个问题,但还是贴一下她提供的解决方案,以备查询。更换Microsoft.XMLHTTP,使用“WinHttp.WinHttpRequest.5.1”,即修改对应程序文件代码如下:
dim url as string
url="https://www.baidu.com"
set xhp=createobject("WinHttp.WinHttpRequest.5.1")
xhp.open "get",url,false
xhp.send
debug.print xhp.responseText
对了,之前采集302跳转真实网址时用到过WinHttp.WinHttpRequest.5.1,它里面有个参数可以阻止跳转,从而提取到302跳转的真实地址。
responeText如有乱码,可以改为如下代码
StrConv(xhp.ResponseBody,vbUnicode)
© 版权声明
文章版权归作者所有,未经允许请勿转载。