求救!用WinHttp获取网页内容失败!

ixibin 2020-01-18 09:11:43
VB6代码如下:

Private Sub Command1_Click()
Dim WinHttp As Object
Dim strTemp As String
Set winHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
winHttp.Open "GET", "https://xin.baidu.com/", True
winHttp.setRequestHeader "Content-Type", "text/html"
winHttp.setRequestHeader "Accept-Language", "zh-cn"
winHttp.setRequestHeader "Connection", "Keep-Alive"
winHttp.setRequestHeader "Accept", "*/*"
winHttp.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; " & _
"Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1))"
winHttp.Send ""
winHttp.WaitForResponse
strTemp = winHttp.ResponseText
Debug.Print strTemp
End Sub


各位高手,上述代码无法获取网页内容(“https://xin.baidu.com/”用浏览器可以正常打开),但是上述代码换其他网页(例如“https://www.baidu.com/”)就能够获取,请问需要怎么弄才能获取呢?谢谢!!!
...全文
781 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
milaoshu1020 2020-02-15
  • 打赏
  • 举报
回复
引用 5 楼 ixibin 的回复:
谢谢 @milaoshu1020 !我还想请教一下能否有办法获得 xin.baidu.com 的内容呢?我实在没办法了


可以试试Python;
ixibin 2020-02-02
  • 打赏
  • 举报
回复
谢谢各位回答,结帖
greatbody 2020-01-31
  • 打赏
  • 举报
回复
给题主提个建议,发提问帖的时候,说明自己的操作系统版本。 如果能将自己的代码打包上传是最好的了。
xulei1024 2020-01-29
  • 打赏
  • 举报
回复
按理是不能的,百度,淘宝都是有防抓取机制的
ixibin 2020-01-25
  • 打赏
  • 举报
回复
谢谢 @milaoshu1020 !我还想请教一下能否有办法获得 xin.baidu.com 的内容呢?我实在没办法了
ixibin 2020-01-19
  • 打赏
  • 举报
回复
引用 1 楼 milaoshu1020 的回复:
Connection: keep-alive Date: Sat, 18 Jan 2020 14:55:36 GMT Content-Length: 0 Content-Type: text/html; charset=UTF-8 Location: http://cas.baidu.com/?action=check&appid=582&rb=1&u=http%3A%2F%2Fxin.baidu.com%2F%3Ffl%3D1 Server: nginx 这是服务器返回的Header,可以继续访问Location指定的地址;
谢谢回复! 请问上面的Header信息在哪里能看到呢?另外我尝试将Location的信息放到代码里,结果运行时报-2147012890错误“URL 未使用可识别的协议”,不知道如何处理了,还望高手不吝指教!!
milaoshu1020 2020-01-19
  • 打赏
  • 举报
回复
WinHttp.GetResponseHeader("Location")
返回Header中的Location;
milaoshu1020 2020-01-19
  • 打赏
  • 举报
回复
这样:

Private Sub Command1_Click()
Dim WinHttp As Object
Dim strTemp As String
Set WinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
WinHttp.Open "GET", "https://xin.baidu.com/", True
WinHttp.setRequestHeader "Content-Type", "text/html"
WinHttp.setRequestHeader "Accept-Language", "zh-cn"
WinHttp.setRequestHeader "Connection", "Keep-Alive"
WinHttp.setRequestHeader "Accept", "*/*"
WinHttp.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; " & _
"Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1))"
WinHttp.Send ""
WinHttp.WaitForResponse
strTemp = WinHttp.GetAllResponseHeaders
Debug.Print strTemp
End Sub
milaoshu1020 2020-01-18
  • 打赏
  • 举报
回复
Connection: keep-alive
Date: Sat, 18 Jan 2020 14:55:36 GMT
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Location: http://cas.baidu.com/?action=check&appid=582&rb=1&u=http%3A%2F%2Fxin.baidu.com%2F%3Ffl%3D1
Server: nginx

这是服务器返回的Header,可以继续访问Location指定的地址;

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧