Excel VBA code to scrape translation from DeepL.com
Budget: $10 – $30 USD
I currently have the following code which allows me to scrape a German to English translation from Google translate using a formula '=translate(A1)' in the spreadsheet.
Public Function Translate(germanStr As String) As String
Dim URLstr As String
Dim objHTTP As Object
Dim objHTML As Object
Dim objDivs As Object, objDiv As Object
' create URL and send
URLstr = "https://translate.google.com/m?hl=de" & "&sl=de" & "&tl=en" & "&ie=UTF-8&prev=_m&q=" & germanStr
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "GET", URLstr, False
objHTTP.Send
' create an html document
Set objHTML = CreateObject("htmlfile")
With objHTML
.Open
.Write objHTTP.responseText
.Close
End With
'get all divs
Set objDivs = objHTML.getElementsByTagName("div")
For Each objDiv In objDivs
If objDiv.className = "result-container" Then
Translate = objDiv.innerText
End If
Next objDiv
Set objHTML = Nothing
Set objHTTP = Nothing
End Function
Since the Google translations are not too exciting I would like to have the code re-written so it scrapes the translation from DeepL.com instead. A sample for the URL would be:
https://www.deepl.com/translator#de/en/Ich%20esse%20guten%20Kuchen.
The result can be found in the source in the following div:
<div id="target-dummydiv" class="lmt__textarea lmt__textarea_dummydiv">She counts the chickens.</div>
The Problem I'm having is that I don't get the page back when I modify above code. Instead I'm getting a "page not found".
A quick turnaround would be appreciated.
Thanks for you help.
Public Function Translate(germanStr As String) As String
Dim URLstr As String
Dim objHTTP As Object
Dim objHTML As Object
Dim objDivs As Object, objDiv As Object
' create URL and send
URLstr = "https://translate.google.com/m?hl=de" & "&sl=de" & "&tl=en" & "&ie=UTF-8&prev=_m&q=" & germanStr
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "GET", URLstr, False
objHTTP.Send
' create an html document
Set objHTML = CreateObject("htmlfile")
With objHTML
.Open
.Write objHTTP.responseText
.Close
End With
'get all divs
Set objDivs = objHTML.getElementsByTagName("div")
For Each objDiv In objDivs
If objDiv.className = "result-container" Then
Translate = objDiv.innerText
End If
Next objDiv
Set objHTML = Nothing
Set objHTTP = Nothing
End Function
Since the Google translations are not too exciting I would like to have the code re-written so it scrapes the translation from DeepL.com instead. A sample for the URL would be:
https://www.deepl.com/translator#de/en/Ich%20esse%20guten%20Kuchen.
The result can be found in the source in the following div:
<div id="target-dummydiv" class="lmt__textarea lmt__textarea_dummydiv">She counts the chickens.</div>
The Problem I'm having is that I don't get the page back when I modify above code. Instead I'm getting a "page not found".
A quick turnaround would be appreciated.
Thanks for you help.