2017-01-03 59 views
1

我目前正在學習託福。我想要做的就是寫出優秀的文字並從網站上用土耳其語重新獲得他們的含義。這是我到目前爲止所做的。從詞典網站檢索數據

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

根據你的回答,這裏有一些改進的代碼,它具有簡單的錯誤處理,並且更容易適應於給定數量的單詞(在你的例子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