根据工作表中的内容从网站获取数据

时间:2015-08-19 13:51:09

标签: excel vba excel-vba web-crawler

        Sub try()
        Dim ie As Object
        Dim MyHTML_Element As IHTMLElement
        Dim MyURL As String
        Dim MyURLSer As String
        Set ie = CreateObject("InternetExplorer.Application")
   On Error GoTo Err_Clear
    MyURL = "[http://test.com/login][1]"
   Set MyBrowser = New InternetExplorer
    MyBrowser.Silent = True
   MyBrowser.navigate MyURL
   MyBrowser.Visible = True
        Do
      Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.j_username.Value = "User" 'Enter your email id here
HTMLDoc.all.j_password.Value = Password'Enter your password here
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Type = "button" Then MyHTML_Element.Click: Exit For
Next
On Error GoTo Err_Clear
MyURL = "http://test.com/home"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

我的问题是,当我尝试输入我想在网站上搜索的号码时,我无法做到这一点。无法输入提交按钮。

<FORM name=menuSearchForm id=menuSearchForm style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px" action=abc/search/default method=post>
<INPUT name=searchKeyword class=search-field id=search value="Enter number" jQuery19101740035784191712="16">
<TD class=search_btn><A style="MARGIN: 0px" onclick=javascript:submitSearch(); href="javascript:void(0);">

1 个答案:

答案 0 :(得分:0)

尝试直接运行javascript。

Call HTMLDoc.parentWindow.execScript("submitSearch();", "javascript")

href元素中的a设置为什么都不做,但onClick设置为运行上述脚本。