Question

Having nicked some code from msdn I'm miffed that it doesn't work exactly as I want straight away. I'm trying to use google translate to, well, translate some stuff for me on the fly. The problem is that t5he responseFromServer doesn't contain the translated text, nor does the source when I look at it using a browser although when looking at the page itself chien is proudly displayed.

void getTranslation()
    {
        WebRequest request = WebRequest.Create("http://translate.google.com/translate_t?hl=en#en|fr|dog");
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        int index = 0;
        while (responseFromServer.Substring(index).Contains("dog"))
        {
            index = responseFromServer.IndexOf("dog", index + 1);
            Console.WriteLine(responseFromServer.Substring(index < 50 ? 0 : index - 50, 100));
            Console.WriteLine(" ");
        }
    }

Does anyone know what I'm failing to understand here? Or of a website that returns a translation as simple as the request?

Was it helpful?

Solution

The reason is that the translation request itself is an asynchronous AJAX request. If you view the source of the page you are trying to retrieve, you won't find the word chien.

You could take a look at the Google AJAX Language API to achieve what you want.

OTHER TIPS

It doesn't work because this application uses javascript to automatically post.

If you want to do this via screenscraping you'll have to do a POST request to the URL of the form with the correct parameters.

However, it would be more advisable for you to just use their API rather than webscraping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top