想要在没有WebBrowser控件的情况下阅读网站正文中的文本 [英] Want to read the text within the body of a web site without a WebBrowser control
问题描述
我需要调用一个网站并将响应变回一个变量(我不希望在我的表单上有一个WebBrowser控件)
I have a need to call a web site and get the response back into a variable (I prefer not to have to have a WebBrowser control on my form)
I我正在使用Access 2013. 我希望不必使用Web浏览器控件(我目前正在使用)
I am using Access 2013. I would like to not have to use a Web Browser Control (as I currently am using)
目前我正在使用:
  vBrowserCommandString =" = nextersdemo.azurewebsites.net / api / v1?UserID =" &安培; Me.TextBoxUserName& "&安培;密码=" &安培; Me.TextBoxPassword
  Me.WebBrowser0.ControlSource = vBrowserCommandString
vBrowserCommandString = "=nextersdemo.azurewebsites.net/api/v1?UserID=" & Me.TextBoxUserName & "&Password=" & Me.TextBoxPassword
Me.WebBrowser0.ControlSource = vBrowserCommandString
Me.TextBoxWebReturn = Me.WebBrowser0.Object.Document
Me.TextBoxWebReturn = Me.WebBrowser0.Object.Document
最后一行不显示网站正文中的文本,但显示文本"[object HTMLDocument]"
The last line does not display the text in the body of the web site but displays the text "[object HTMLDocument]"
我需要网页正文中包含的文字。
I need the text that is contained within the body of the web page.
推荐答案
为什么不使用MSXML?  类似于:
Why not use MSXML? Something like:
'sURL -> URL to extract, do not forget the http://www. part
Function GetSiteHTML(sURL As String) As String
On Error GoTo Error_Handler
Dim oMSXML As Object 'SHDocVw.InternetExplorer
Set oMSXML = CreateObject("MSXML2.XMLHTTP")
With oMSXML
.Open "GET", sURL, False
.send
End With
GetSiteHTML = oMSXML.responseText
Error_Handler_Exit:
On Error Resume Next
If Not oMSXML Is Nothing Then Set oMSXML = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: GetSiteHTML" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function
然后你可以简单地称之为:
Then you could simply call it like:
sHTML = GetSiteHTML("http://nextersdemo.azurewebsites.net/api/v1?UserID=" & Me.TextBoxUserName & "&Password=" & Me.TextBoxPassword)
这篇关于想要在没有WebBrowser控件的情况下阅读网站正文中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!