Html VBA将网页中的数据抓取到变量中

Html VBA将网页中的数据抓取到变量中,html,excel,getelementsbyname,vba,Html,Excel,Getelementsbyname,Vba,我能够通过vba将excel值传递到网站并单击它。但它打开了另一个标题为“结果-研究随机化器”的页面,我不知道如何在“集合1”中检索这些值。谁能给我一些想法,将这些值检索到一个变量中。我的代码是 Sub OpenPage() Const myPageTitle As String = "Research Randomizer Form v4.0" Const myPageURL As String = "http://www.randomizer.org/form.htm" Dim No

我能够通过vba将excel值传递到网站并单击它。但它打开了另一个标题为“结果-研究随机化器”的页面,我不知道如何在“集合1”中检索这些值。谁能给我一些想法,将这些值检索到一个变量中。我的代码是

Sub OpenPage()
Const myPageTitle As String = "Research Randomizer Form v4.0"
Const myPageURL As String = "http://www.randomizer.org/form.htm"
    Dim NoofSet, NoPSet, RangeBeg, RangeEnd As String
    NoofSet = Range("b3").Value
    NoPSet = Range("c3").Value
    RangeBeg = Range("d3").Value
    RangeEnd = Range("e3").Value

    Dim myIE As SHDocVw.InternetExplorer
    Dim doc As HTMLDocument
    Dim PageForm As HTMLFormElement
    Dim UserIdBox As HTMLInputElement
    Dim PasswordBox As HTMLInputElement
    Dim HrangeBeg, HrangeEnd As HTMLInputElement
    Dim FormButton As HTMLInputButtonElement
    Dim Elem As IHTMLElement


    'check if page is already open
  Set myIE = GetOpenIEByTitle(myPageTitle, False)
     If myIE Is Nothing Then
    'page isn't open yet
    'create new IE instance
    Set myIE = GetNewIE
    'make IE window visible
    myIE.Visible = True
    'load page
    If LoadWebPage(myIE, myPageURL) = False Then
      'page wasn't loaded
      MsgBox "Couldn't open page"
      Exit Sub
    End If
  End If

    Do
    DoEvents
    Loop Until myIE.readyState = READYSTATE_COMPLETE

    Set doc = myIE.document
    Set PageForm = doc.forms(0)
    'Get the User Id textbox
    '< input class="TextBox" maxlength="15" name="UserName" size="12">

    Set UserIdBox = PageForm.elements("numofsets")
    'Set the User Id
    UserIdBox.Value = NoofSet

    'Get the password textbox
    '< input class="TextBox" type="password" maxlength="10" name="Password" size="12">
    Set PasswordBox = PageForm.elements("numperset")
    'Set the password
    PasswordBox.Value = NoPSet

    Set HrangeBeg = PageForm.elements("rangebeg")
    HrangeBeg.Value = RangeBeg
    Set HrangeEnd = PageForm.elements("rangeend")
    HrangeEnd.Value = RangeEnd


    'Submit the form (like clicking the 'Submit' button) to navigate to next page

    PageForm.Button.Click

    'Wait for the new page to load

    Do
    DoEvents
    Loop Until myIE.readyState = READYSTATE_COMPLETE
    myIE.Visible = True
'Working fine till here
'Need to pull the data from the 2nd webisite





    End Sub

'returns new instance of Internet Explorer
Function GetNewIE() As SHDocVw.InternetExplorer
  'create new IE instance
  Set GetNewIE = New SHDocVw.InternetExplorer
  'start with a blank page
  GetNewIE.Navigate2 "about:Blank"
End Function

'loads a web page and returns True or False depending on
'whether the page could be loaded or not
Function LoadWebPage(i_IE As SHDocVw.InternetExplorer, _
                     i_URL As String) As Boolean
  With i_IE
    'open page
    .navigate i_URL
    'wait until IE finished loading the page
    Do While .readyState <> READYSTATE_COMPLETE
      Application.Wait Now + TimeValue("0:00:01")
    Loop
    'check if page could be loaded
    If .document.URL = i_URL Then
      LoadWebPage = True
    End If
  End With
End Function

'finds an open IE site by checking the URL
Function GetOpenIEByURL(ByVal i_URL As String) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

  'ignore errors when accessing the document property
  On Error Resume Next
  'loop over all Shell-Windows
  For Each GetOpenIEByURL In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByURL.document) = "HTMLDocument" Then
      'check the URL
      If GetOpenIEByURL.document.URL = i_URL Then
        'leave, we found the right window
        Exit Function
      End If
    End If
  Next
End Function

'finds an open IE site by checking the title
Function GetOpenIEByTitle(i_Title As String, _
                          Optional ByVal i_ExactMatch As Boolean = True) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

  If i_ExactMatch = False Then i_Title = "*" & i_Title & "*"
  'ignore errors when accessing the document property
  On Error Resume Next
  'loop over all Shell-Windows
  For Each GetOpenIEByTitle In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByTitle.document) = "HTMLDocument" Then
      'check the title
      If GetOpenIEByTitle.document.Title Like i_Title Then
        'leave, we found the right window
        Exit Function
      End If
    End If
  Next
End Function
子OpenPage()
Const myPageTitle As String=“研究随机化器表单v4.0”
常量myPageURL作为字符串=”http://www.randomizer.org/form.htm"
Dim NoofSet、NoPSet、RangeBeg、RangeEnd作为字符串
NoofSet=范围(“b3”).值
NoPSet=范围(“c3”).值
RangeBerg=范围(“d3”).值
RangeEnd=范围(“e3”).值
Dim myIE作为SHDocVw.InternetExplorer
作为HTMLDocument的Dim doc
将PageForm设置为HTMLFormElement
作为HTMLInputElement的Dim UserIdBox
将密码框设置为HTMLInputElement
Dim HrangeBeg,HrangeEnd作为HTMLInputElement
将FormButton调暗为HTMLInputButtonElement
按IHTMLElement进行调暗
'检查页面是否已打开
设置myIE=GetOpenIEByTitle(myPageTitle,False)
如果myIE什么都不是
“页面尚未打开
'创建新的IE实例
设置myIE=GetNewIE
“使窗户可见
myIE.Visible=True
'加载页面
如果LoadWebPage(myIE,myPageURL)=False,则
“页面未加载
MsgBox“无法打开页面”
出口接头
如果结束
如果结束
做
多芬特
循环直到myIE.readyState=readyState\u完成
Set doc=myIE.document
设置页面表单=单据表单(0)
'获取用户Id文本框
“
Set UserIdBox=PageForm.elements(“numofset”)
'设置用户Id
UserIdBox.Value=NoofSet
'获取密码文本框
“
Set PasswordBox=PageForm.elements(“numperset”)
'设置密码
PasswordBox.Value=NoPSet
Set HrangeBeg=PageForm.elements(“rangebeg”)
hrangeberg.Value=RangeBeg
Set HrangeEnd=PageForm.elements(“rangeend”)
HrangeEnd.Value=RangeEnd
'提交表单(如单击“提交”按钮)以导航到下一页
PageForm.Button.Click
'等待加载新页面
做
多芬特
循环直到myIE.readyState=readyState\u完成
myIE.Visible=True
“干得很好,一直干到这里
'需要从第二个网站中提取数据
端接头
'返回Internet Explorer的新实例
函数GetNewIE()作为SHDocVw.InternetExplorer
'创建新的IE实例
设置GetNewIE=New SHDocVw.InternetExplorer
'从空白页开始
GetNewIE.Navigate2“关于:空白”
端函数
'加载网页并返回True或False,具体取决于
'是否可以加载页面
函数加载网页(即SHDocVw.InternetExplorer_
i_URL作为字符串)作为布尔值
和我一起
“打开页面
.浏览i_URL
“请等到IE加载完页面后再说
请稍候。readyState readyState\u完成
应用程序。立即等待+时间值(“0:00:01”)
环
'检查是否可以加载页面
如果.document.URL=i\u URL,则
LoadWebPage=True
如果结束
以
端函数
'通过检查URL查找打开的IE站点
函数GetOpenIEByURL(ByVal i_URL作为字符串)作为SHDocVw.InternetExplorer
将objShellWindows变暗为新SHDocVw.ShellWindows
'访问文档属性时忽略错误
出错时继续下一步
'在所有外壳窗口上循环
对于objShellWindows中的每个GetOpenIEByURL
'如果文档类型为HTMLDocument,则它是一个IE窗口
如果TypeName(GetOpenIEByURL.document)=“HTMLDocument”,则
'检查URL
如果GetOpenIEByURL.document.URL=i\u URL,则
“离开,我们找到了正确的窗口
退出功能
如果结束
如果结束
下一个
端函数
'通过检查标题查找打开的IE站点
函数GetOpenIEByTitle(i_Title作为字符串_
可选的ByVal i_ExactMatch作为Boolean=True)作为SHDocVw.InternetExplorer
将objShellWindows变暗为新SHDocVw.ShellWindows
如果i_ExactMatch=False,则i_Title=“*”&i_Title&“*”
'访问文档属性时忽略错误
出错时继续下一步
'在所有外壳窗口上循环
对于objShellWindows中的每个GetOpenIEByTitle
'如果文档类型为HTMLDocument,则它是一个IE窗口
如果TypeName(GetOpenIEByTitle.document)=“HTMLDocument”,则
“检查标题
如果GetOpenIEByTitle.document.Title与i\U Title类似,则
“离开,我们找到了正确的窗口
退出功能
如果结束
如果结束
下一个
端函数

此代码将找到并标识打开的“结果”窗口,然后将其背后的源代码分配给一个变量(my_var)。然后可以从变量中提取所需内容

' Find the open instance of IE that contains the "Results"
    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count

    For x = 0 To (IE_count - 1)
        On Error Resume Next
        my_url = objShell.Windows(x).document.Location
        my_title = objShell.Windows(x).document.Title

        If my_title Like "Results - Research Randomizer" Then
            Set ie = objShell.Windows(x)
            Exit For
        Else
        End If
    Next

    my_var = ie.document.body.innerhtml

你有没有试过在你发布的代码末尾的函数?似乎直接在Excel中执行此操作比使用网页更容易…这基本上是OP发布代码的最后一部分(但他的函数不返回结果…)是的,它给出了结果。我把它粘贴在代码的末尾,现在我的_var存储了整个网页。我可以接受。我必须通过excel的“搜索”和“mid”功能分离“Set1”中的值,最后将这些值存储在excel单元格中。我的工作可以用这个代码完成。但是如果Set1的值可以存储在我的_var中,而不是获取整个身体,那就更好了。同样,我考虑过用excel本身计算随机化计算,但我不熟悉他们在网站上使用的计算。最后,为你的生日添上一份礼物interest@user3051587如果这个答案被证明是有用的,你应该考虑一下。只需添加下面的代码,MyOffice结果将包含SIT1数据>代码> set结果=IE.Doop.GETelEngsByTAGNITY(“表”),如果结果为每一个ITM,则ISTC(1,ITM.NeNTENT,“SETYA”,VBTrTrimeApple)。然后my_results=itm.innertext退出结束,如果下一个itm