2019年7月5日 星期五
VBA抓網站資訊(.setRequestHeader)心得
Dim HTMLsourcecode As Object, Clipboard As Object, Getxml As Object
Set HTMLsourcecode = CreateObject("htmlfile")
Set Clipboard = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set Getxml = CreateObject("WinHttp.WinHttpRequest.5.1")
With Getxml
.Open "POST", url, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Referer", url_referer
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.Send
------------------------------------------------------------------------------------------------
使用POST抓到網頁資料的方法,關鍵在於要設定
.setRequestHeader "Referer", url_referer
有的網站會檢查來源網址是哪來的,這時就要把來源網址給填入進去
------------------------------------------------------------------------------------------------
而平常為了模擬瀏覽器抓資料,請加入
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
------------------------------------------------------------------------------------------------
抓網頁最新資料,而不是網站快取檔案(Cache)
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
------------------------------------------------------------------------------------------------
"Content-Type"
標示網路文件類型和網頁編碼,決定瀏覽器將以什麼形式、什麼編碼讀取這個文件
"application/x-www-form-urlencoded"
POST傳送表單時,使用該編碼格式(使用Postost傳送資料,這行一定要加)
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言