Html 如何从新的谷歌翻译网站上引用和检索翻译结果?(使用VBA)

Html 如何从新的谷歌翻译网站上引用和检索翻译结果?(使用VBA),html,excel,vba,web-scraping,google-translate,Html,Excel,Vba,Web Scraping,Google Translate,目前,这是我在IE对象中加载网站(从仍然正确显示google translate的生成URL)后(在google更新google translate网站的第二天,我已经2天没有接触过该文件并尝试过)从google translate获得结果的代码(成功): 这一行现在给出了上周四(11/29)谷歌更新谷歌翻译网站时的一个错误: Function GetTransItem(IE As Object) As String Dim strInnerHTML As String Dim ArraySp

目前,这是我在IE对象中加载网站(从仍然正确显示google translate的生成URL)后(在google更新google translate网站的第二天,我已经2天没有接触过该文件并尝试过)从google translate获得结果的代码(成功):

这一行现在给出了上周四(11/29)谷歌更新谷歌翻译网站时的一个错误:

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
strInnerHTML = IE.Document.getElementById("result_box").innerHTML
HTML现在完全不同了,我不知道我需要“获取”什么而不是“结果框”来找到网站上的翻译文本

如果有人能帮我找出哪行代码将返回包含翻译结果的字符串,我将不胜感激。

它看起来像是一个:

“翻译API

谷歌还有一个更强大的付费服务。Translate API可用于为web应用程序提供动力,也可用于翻译网站内容。”

"

翻译API是一个易于使用的Google REST API。您不必从文档中提取文本,只需发送HTML文档并返回翻译文本即可。“

翻译:

  • Google Speak:“您不必从文档中提取文本…”

  • 中文:“您没有权限,无法再从文档中提取文本…”

:

“有免费配额吗?
不,云翻译API仅作为付费服务提供。有关更多详细信息,请参阅。“


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

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

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
选项显式
公共子GetInfo()
Dim IE作为新的InternetExplorer,t作为日期,剪贴板作为对象,ws作为工作表
常量最大等待时间=5秒
设置ws=ThisWorkbook.Worksheets(“Sheet1”)
设置剪贴板=GetObject(“新建:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}”)
与IE
.Visible=True
.导航“https://translate.google.com/#view=home&op=translate&sl=auto&tl=en"
当.Busy或.readyState<4:DoEvents:Wend时
.document.querySelector(“#source”).Value=“你好”
将hTable设置为HTMLTable
t=计时器
做
出错时继续下一步
Set hTable=.document.querySelector(“.gt baf table”)
错误转到0
如果定时器-t>最大等待时间,则退出Do
循环,而hTable什么都不是
如果不是hTable,那就什么都不是了
剪贴板.SetText hTable.outerHTML
剪贴板.PutInClipboard
ws.Cells(1,1).PasteSpecial
如果结束
退出
以
端接头

使用本地版本HTML检查右侧翻译表的类名:


从结果框中:

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
选项显式
公共子GetInfo()
Dim IE作为新的InternetExplorer,t作为日期,ws作为工作表
常量最大等待时间=5秒
设置ws=ThisWorkbook.Worksheets(“Sheet1”)
与IE
.Visible=True
.导航“https://translate.google.com/#view=home&op=translate&sl=auto&tl=en"
当.Busy或.readyState<4:DoEvents:Wend时
.document.querySelector(“#source”).Value=“je vous remercie”
将平移设置为对象,将平移文本设置为字符串
t=计时器
做
出错时继续下一步
Set translation=.document.querySelector(“.tlid translation.translation”)
translationText=translation.textContent
错误转到0
如果定时器-t>最大等待时间,则退出Do
translationText=vbNullString时循环
ws.Cells(1,1)=translationText
退出
以
端接头

是的,我在新的网站构建中没有看到任何ID-您必须使用
GetElementsByTagName
并运行divs以获得新的
文本包装tlid副本目标。谢谢!我要给这个打个电话shot@QHarr .. 非常感谢。我找不到此类“.gt baf table”,并且没有复制到单元格中A1@YasserKhalil你可能有局部变异。我将发布html图像。首先,阿拉伯语部分被选中,所以我得到了错误。但当手动打开IE浏览器并选择英语部分时。然后我运行了代码它对我来说很好。。那么,有没有一种默认选择英语的方法呢?谢谢,我能够在代码中找到正确的位置,但是看到所有这些放在一起是很有帮助的,谢谢。另外,使用HTML库而不是像我那样大量的字符串操作更有效。感谢您添加此建议。我去年使用的代码已经停止工作,我觉得这很有帮助。是一种替代方法,在撰写本文时,它仍然可以使用VBA,并且比使用IE更快。