2016-08-24 23 views
1
创建对象
Sub Get_Data() 


Set ie = CreateObject("InternetExplorer.Application") 
ie.Visible = True 
ie.Navigate "http://www.scramble.nl/military-database/usaf" 
Do While ie.Busy 
    Application.Wait DateAdd("s", 1, Now) 
Loop 
SendKeys "03-3114" 
SendKeys "{ENTER}" 


End Sub 

下面搜索键盘输入值03-3114的代码,并在表中获取数据。如果我想搜索单元格A1中已经存在的值,并在单元格范围(“B1:E1”)中从表格中为“代码,类型,CN,单元”刮取值,我该怎么办?网页抓取 - 为IE

+0

嗨,你好,你能解释一下从哪个表中,U闯民宅.. – HA560

+0

@ HA560我的意思是tr.rowBoard表我有要导入从该表中的任何值成问题excel cell ... –

回答

0

您使用的是SendKeys这是非常不可靠:)为什么不找到文本框的名称和搜索按钮,并直接与它进行交互,如下所示?

Sub Get_Data() 
    Dim ie As Object, objInputs As Object 

    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = True 
    ie.Navigate "http://www.scramble.nl/military-database/usaf" 

    Do While ie.readystate <> 4: DoEvents: Loop 

    '~~> Get the ID of the textbox where you want to output 
    ie.Document.getElementById("serial").Value = "03-3114" 

    '~~> Here we try to identify the search button and click it 
    Set objInputs = ie.Document.getElementsByTagName("input") 
    For Each ele In objInputs 
     If ele.Name Like "sbm" Then 
      ele.Click 
      Exit For 
     End If 
    Next 
End Sub 

注意:了解我是怎么得到的名字serialsbm,指略低于图像上面给出的解释。

下面的代码搜索键盘类型值03-3114并获取表中的数据。如果我想搜索单元格A1中已经存在的值,并在单元格范围(“B1:E1”)中从表格中为“代码,类型,CN,单元”刮取值,我该怎么办?

直接就把值从A1代替硬编码值

ie.Document.getElementById("serial").Value = Sheets("Sheet1").Range("A1").Value 

的要想从表中的值,确定通过右键单击该表的元素就可以在浏览器中点击“检查/检查元素(在Chrome中,它只是Inspect)”,如下所示。

enter image description here

我可以给你的代码,但我希望你能自己做。如果你仍然坚持,那么用你试过的代码更新问题,然后我们会从那里拿到它。

有趣的阅读html parsing of cricinfo scorecards

+0

太棒了! @ Siddharth Rout,我很难从web表(tr.rowBoard)将代码值导入到Excel中的B1单元格中,我认为Xpath是一个问题,但xpath // * [@ id =“wideContent”]/div [5]/div [2]/div [1]/table/tbody/tr [3]/td [3]/span我从Chrome中的检查元素 –