xParaDoX
xParaDoX

Reputation: 1

C# Grabbing text from a span

I tried this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Danish to English: ");
            string tittyfuck = Console.ReadLine();
            Console.Beep();
            WebRequest webRequest = new WebRequest.Create("http://translate.google.com/#da/en/" + tittyfuck);
            WebResponse webResponse = webRequest.GetResponse();

            Stream data = webResponse.GetResponseStream();
            string html;

            using (StreamReader streamReader = new StreamReader(data))
            {
                string line;
                while ((line = streamReader.ReadLine() != null))
                {
                    if (line == "<span class=\"hps\">")
                    {
                        Console.Beep();
                        Console.WriteLine(line);
                    }
                }
            }
        }
    }
}

Okay, so I try that but I get these errors:

Error 1 'System.Net.WebRequest.Create(System.Uri)' is a 'method' but is used like a 'type' C:\Users\Dylan\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 18 52 ConsoleApplication1

and

Error 2 Cannot implicitly convert type 'bool' to 'string' C:\Users\Dylan\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 27 32 ConsoleApplication1

As you can probably tell, I'm trying to open a request to translate.google.com with the text after the link, which then grabs the text that's printed to the which is the translated text.. It's basically a translator. Please help.

Upvotes: 0

Views: 462

Answers (3)

VladL
VladL

Reputation: 13043

Line 18:

WebRequest webRequest = WebRequest.Create(new URI("http://translate.google.com/#da/en/" + tittyfuck));

Line 27:

while ((line = streamReader.ReadLine()) != null)

Upvotes: 1

Sam
Sam

Reputation: 2970

You can't use google translate in this way because the translation is requested by javascript, you can try with a webbrowser or buying some characters for using the translate api

Another way is parsing the result of the request(http://translate.google.com/translate_a/t?....) , that is in json style

Upvotes: 0

SynerCoder
SynerCoder

Reputation: 12776

Remove the new keyword, and set other parenthesis.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Danish to English: ");
            string tittyfuck = Console.ReadLine();
            Console.Beep();
            WebRequest webRequest = WebRequest.Create("http://translate.google.com/#da/en/" + tittyfuck);
            WebResponse webResponse = webRequest.GetResponse();

            Stream data = webResponse.GetResponseStream();
            string html;

            using (StreamReader streamReader = new StreamReader(data))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    if (line == "<span class=\"hps\">")
                    {
                        Console.Beep();
                        Console.WriteLine(line);
                    }
                }
            }
        }
    }
}

Upvotes: 0

Related Questions