在用WinHttp寫網路爬蟲時,最怕遇到一種錯:
對於非資訊背景的我,即使找到了相關的文件,也猶如在看天書一樣完全無法理解
有興趣的可參考:
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/dn786418(v=ws.11)
例如在抓ptt八卦版資料,就會遇到這種錯
Sub test()
Dim myXML As Object
Set myXML = CreateObject("WinHttp.WinHttpRequest.5.1")
With myXML
.Open "GET", "https://www.ptt.cc/bbs/Gossiping/index.html", False
.setRequestHeader "cookie", "over18=1"
.send
End With
End Sub
如果是使用Microsoft.XMLHTTP物件,當遇到憑證錯誤時會跳出
這倒還好,因為只要按下"是"就可以繼續運行,但偏偏Ptt八卦版是要設定cookie才能抓到資料,所以一定要使用WinHttp物件
幸好最近找到2種解法可以忽略掉憑證錯誤
寫法一:
Sub test()
Dim myXML As Object
Set myXML = CreateObject("WinHttp.WinHttpRequest.5.1")
With myXML
.Open "GET", "https://www.ptt.cc/bbs/Gossiping/index.html", False
.setRequestHeader "cookie", "over18=1"
.Option(4) = 13056
.send
End With
End Sub
寫法二:
Sub test()
Dim myXML As Object
Set myXML = CreateObject("WinHttp.WinHttpRequest.5.1")
With myXML
.Open "GET", "https://www.ptt.cc/bbs/Gossiping/index.html", False
.setRequestHeader "cookie", "over18=1"
.Option(4) = &H3300
.send
End With
End Sub
終於解決困擾已久的問題
沒有留言:
張貼留言