如何从新 google 翻译网站引用和检索翻译结果? (使用 VBA)

How to reference and retrieve translation result from new google translate website? (using VBA)

目前这是我使用(成功)从 google translate 加载网站后获得结果的代码(来自生成的 URL 仍然会出现 google translate正确)在 IE 对象中(我已经 2 天没有触及该文件,并在 google 更新他们的 google 翻译网站后的第二天尝试了它):

Function GetTransItem(IE As Object) As String

Dim strInnerHTML As String
Dim ArraySplitHTML
Dim iArrayItem As Long
Dim strTranslated As String

strInnerHTML = IE.Document.getElementById("result_box").innerHTML

'some other code here to fix hmtl character encodings, clean up HTML, etc. etc.

GetTransItem = strTranslated

End Function

从上周四 (11 月 29 日) 开始,当 google 更新了 google 翻译网站时,此行现在出现错误:

strInnerHTML = IE.Document.getElementById("result_box").innerHTML

HTML 现在完全不同了,我不知道我需要什么 "GET" 而不是 "result_box" 在网站上找到翻译的文本。

如果有人能帮我弄清楚哪一行代码 return 我会得到带有翻译结果的字符串,我将不胜感激。

好像是paid service now:

"Translate API

Google also has a more robust paid offering. The Translate API can be used to power web apps, as well as translate website content.".

"Simple integration

Translation API is an easy-to-use Google REST API. You don’t have to extract text from your document, just send it HTML documents and get back translated text.".

翻译:

  • Google 说:“您不必从文档中提取文本...”。

  • 英语:“您没有 不再能够从您的文档中提取文本.. .".

Google's Translation FAQ:

"Is there any free quota?
No, the Cloud Translation API is only available as a paid service. Please see Pricing for more details.".

从 URL 返回的结果现在变得更难 scrape,以防止绕过付费服务。免费翻译仍然可供人类使用。

您仍然可以使用浏览器检索翻译信息。这只是为了演示抓取右侧的翻译 table。

Option Explicit

Public Sub GetInfo()
    Dim IE As New InternetExplorer, t As Date, clipboard As Object, ws As Worksheet
    Const MAX_WAIT_SEC As Long = 5

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

    With IE
        .Visible = True
        .navigate "https://translate.google.com/#view=home&op=translate&sl=auto&tl=en"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.querySelector("#source").Value = "Bonjour"

        Dim hTable As HTMLTable
        t = Timer
        Do
            On Error Resume Next
            Set hTable = .document.querySelector(".gt-baf-table")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While hTable Is Nothing
        If Not hTable Is Nothing Then
            clipboard.SetText hTable.outerHTML
            clipboard.PutInClipboard
            ws.Cells(1, 1).PasteSpecial
        End If
        .Quit
    End With
End Sub

与您本地版本HTML class翻译名称table右侧:


来自结果框:

Option Explicit
Public Sub GetInfo()
    Dim IE As New InternetExplorer, t As Date, ws As Worksheet
    Const MAX_WAIT_SEC As Long = 5

    Set ws = ThisWorkbook.Worksheets("Sheet1")

    With IE
        .Visible = True
        .navigate "https://translate.google.com/#view=home&op=translate&sl=auto&tl=en"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.querySelector("#source").Value = "je vous remercie"

        Dim translation As Object, translationText As String
        t = Timer
        Do
            On Error Resume Next
            Set translation = .document.querySelector(".tlid-translation.translation")
            translationText = translation.textContent
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While translationText = vbNullString

        ws.Cells(1, 1) = translationText
       .Quit
    End With
End Sub