HTML Scraper from Google Translate

I want to translate a string using Google Translator.

My sample string "this is my string".

I want to use HTML Agility Pack to parse HTML documents.

I tried this:

using HtmlAgilityPack; 

........

var webGet = new HtmlWeb();
var document = webGet.Load(
    "http://translate.google.com/#en/bn/this%20is%20my%20string");

var node = document.DocumentNode.SelectNodes(
    "//span[@class='short_text' and @id='result_box']");

if (node != null)
{
    foreach (var xx in node)
    {
        x = xx.InnerText;
        MessageBox.Show(x);
    }
}

But I do not get any results.

My goal is to translate the complete string using Google Translate and show the translated string in a shortcut in Windows Forms.

How can i do this?

+1
source share
3 answers

. , Google . Google , , , . , -, Google, script . , Google.

2017 : API- Microsoft Translator .

+3

HTML

using System;
using HtmlAgilityPack;    
class Traslator
    {
        private string url;
        private HtmlWeb web;
        private HtmlDocument htmlDoc;

        public Translator(string langPair) // LangPair = "SL|TL" ( Source Lang | Target Lang - Ex.: "en|pt"
        {
            this.url = "http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair=" + langPair;
            this.web = new HtmlWeb();
            this.htmlDoc = new HtmlDocument();
        }

        public string Translate(string input)
        {
            this.htmlDoc = web.Load(String.Format(this.url, Uri.EscapeUriString(input)));
            HtmlNode htmlNode = htmlDoc.DocumentNode.SelectSingleNode("//*[@id=\"result_box\"]");
            return htmlNode.InnerText;
        }
    }

: url used... . Text prop, html, webGet... u se, span.result_box .

+1

, , API, Google .

Update:

, , translate Ajax . , HtmlWeb, - JS, . , .

0

All Articles