从字典网站检索数据

时间:2017-01-03 12:40:38

标签: excel-vba vba excel

我目前正在攻读TOEFL。我想要做的是将文字写入excel并从网站上检索土耳其文的含义。这是我到目前为止所做的。

Sub tureng()

Dim ieObj

Set ieObj = CreateObject("InternetExplorer.Application")
Dim kelime As String
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Sayfa1")
For i = 1 To sht.Range("A10000").End(3).Row
    kelime = sht.Cells(i, 1).Value

  With ieObj
    .Visible = True
    .Navigate "http://tureng.com/tr/turkce-ingilizce/" & kelime
    Do While .Busy Or .readyState <> 4
      DoEvents
    Loop
  End With
Next

End Sub

使用这些代码,我可以打开所需的网站,但我不知道如何获得意义。我只想得到前两三个字。含义与主词在同一行,但它们可以在不同的列中。

1 个答案:

答案 0 :(得分:1)

根据您的回答,这里有一些改进的代码,它具有简单的错误处理,并且更容易适应使用给定数量的单词(在您的示例3中)。请注意,这是未经测试的,但是基于您说的工作代码(以及您之后删除的代码)......

Sub tureng()

    Dim ieObj As Object

    Set ieObj = CreateObject("InternetExplorer.Application")
    Dim allRowOfData As Object
    Dim kelime As String
    Dim sht As Worksheet

    ' If you are using the values immediately in your sheet, you don't need to store them

    Set sht = ThisWorkbook.Sheets("Sayfa1")

    Dim i as Integer
    Dim j as Integer

    For i = 1 To sht.Range("A10000").End(3).Row

        kelime = sht.Cells(i, 1).Value

        With ieObj
            .Visible = False
            .Navigate "http://tureng.com/tr/turkce-ingilizce/" & kelime
            Do While .Busy Or .readyState <> 4
            DoEvents
            Loop
            Set allRowOfData = ieObj.document.getElementsByClassName("tr ts")

            ' Very simple error handling code, simply try 3 times regardless of failure
            On Error Resume Next
            For j = 1 to 3 ' Loop to get up to 3 values

                sht.Cells(i, j+1).Value = allRowOfData(j).innertext

            Next j
            On Error GoTo 0

        End With

    Next I

    ieObj.Quit

End Sub