VBA 新手。我正在嘗試使用我在本 [教程][1] 中看到的方法來訪問以下網頁中的一些資料。https://my.powerdiary.com/Profile/Client/Search
但是,需要登錄,不知道如何通過。我已經完成了我的研究,但是從我在網上找到的任何內容來看,我似乎很難理解我將如何登錄到該頁面。
任何人都可以幫助我或指導我嗎?
選項顯式
Sub HTMLTest()
Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLInputUser As MSHTML.IHTMLElement
Dim HTMLInputPass As MSHTML.IHTMLElement
Dim HTMLRoles As MSHTML.IHTMLElementCollection
Dim HTMLRole As MSHTML.IHTMLElement
XMLReq.Open "GET", "https://my.powerdiary.com/Profile/Client/Search", False, "email", "password"
XMLReq.send
If XMLReq.Status <> 200 Then
MsgBox "Problem" & vbNewLine & XMLReq.Status & "-" & XMLReq.statusText
Exit Sub
End If
HTMLDoc.body.innerHTML = XMLReq.responseText
Set HTMLRoles = HTMLDoc.getElementsByClassName("form-section-title mt10")
Debug.Print HTMLRoles.Length
For Each HTMLRole In HTMLRoles
Debug.Print HTMLRole.tagName, HTMLRole.innerText, HTMLRole.innerHTML
Next HTMLRole
End Sub
我添加的螢屏截圖來自登錄后的頁面。因此,嘗試通過嘗試回傳這些元素來檢查我是否已登錄。
[1]: https://www.youtube.com/watch?v=sGw6r5GVA5g&ab_channel=WiseOwlTutorials! [在此處輸入圖片描述]( https://i.stack.imgur.com/WeThW.png )
uj5u.com熱心網友回復:
當沒有可測驗的憑據時,很難提供任何解決方案,因此我試圖提供的解決方案是假設性的。
根據開發工具,您必須填寫以下三個鍵的值,然后才能將它們作為引數與 post 請求一起發送:
__RequestVerificationToken:
UserName:
Password:
RememberMe: false
MobileSite: false
您需要先發送get請求以收集令牌的值。一旦正確填寫了欄位,您需要對它們進行編碼。
腳本應該是這樣的:
Sub ScrapeAfterLogIn()
Const logInUrl = "https://my.powerdiary.com/Account/Login?ReturnUrl=/Profile/Client/Search"
Const contentPage = "https://my.powerdiary.com/Profile/Client/Search"
Dim oHttp As Object, Html As HTMLDocument, formData$, sToken$
Dim sUser$, sPass$, sResp$
Set Html = New HTMLDocument
Set oHttp = CreateObject("MSXML2.XMLHTTP")
sUser = "" 'your username
sPass = "" 'your password
With oHttp
.Open "GET", logInUrl, False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
.send
Html.body.innerHTML = .responseText
sToken = Html.querySelector("input[name='__RequestVerificationToken']").getAttribute("value")
formData = "__RequestVerificationToken=" & Application.WorksheetFunction.EncodeURL(sToken) & "&UserName=" & Application.WorksheetFunction.EncodeURL(sUser) & "&Password=" & Application.WorksheetFunction.EncodeURL(sPass) & "&RememberMe=false&MobileSite=false"
.Open "POST", logInUrl, False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
.send (formData)
.Open "GET", contentPage, False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
.send
sResp = .responseText
End With
Debug.Print sResp
End Sub
如果由于某種原因您沒有從所需頁面獲取內容,您希望在腳本中使用 cookie 來實作這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/320999.html
