C#从跨距中抓取文本
我试过这个:C#从跨距中抓取文本,c#,webrequest,webresponse,C#,Webrequest,Webresponse,我试过这个: 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)
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);
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
Net系统;
使用System.IO;
使用System.Net.Sockets;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“丹麦语到英语:”);
字符串tittyfuck=Console.ReadLine();
Console.Beep();
WebRequest WebRequest=新建WebRequest.Create(“http://translate.google.com/#da/en/“+tittyfuck);
WebResponse WebResponse=webRequest.GetResponse();
流数据=webResponse.GetResponseStream();
字符串html;
使用(StreamReader StreamReader=新StreamReader(数据))
{
弦线;
而((line=streamReader.ReadLine()!=null))
{
如果(行==“”)
{
Console.Beep();
控制台写入线(行);
}
}
}
}
}
}
好的,我尝试了一下,但我得到了以下错误:
错误1“System.Net.WebRequest.Create(System.Uri)”是一个“方法”,但与“类型”C:\Users\Dylan\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 18 52 ConsoleApplication1一样使用
及
错误2无法将类型“bool”隐式转换为“string”C:\Users\Dylan\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 27 32 ConsoleApplication1
正如你可能知道的,我正试图打开一个请求,在链接后用文本翻译google.com,然后获取打印到的文本,这就是翻译文本。。它基本上是一个翻译。
请帮助。第18行:
WebRequest webRequest = WebRequest.Create(new URI("http://translate.google.com/#da/en/" + tittyfuck));
第27行:
while ((line = streamReader.ReadLine()) != null)
删除新关键字,并设置其他括号
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);
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
Net系统;
使用System.IO;
使用System.Net.Sockets;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“丹麦语到英语:”);
字符串tittyfuck=Console.ReadLine();
Console.Beep();
WebRequest-WebRequest=WebRequest.Create(“http://translate.google.com/#da/en/“+tittyfuck);
WebResponse WebResponse=webRequest.GetResponse();
流数据=webResponse.GetResponseStream();
字符串html;
使用(StreamReader StreamReader=新StreamReader(数据))
{
弦线;
while((line=streamReader.ReadLine())!=null)
{
如果(行==“”)
{
Console.Beep();
控制台写入线(行);
}
}
}
}
}
}
您不能以这种方式使用google translate,因为翻译是由javascript请求的,您可以尝试使用webbrowser或购买一些字符来使用translate api
另一种方法是解析请求(.)的结果,这是json风格的您的字符串有奇怪的命名约定…
新的WebRequest.Create
应该是WebRequest.Create
@Stijn我试过了,但得到了错误1无法创建抽象类或接口“System.Net.WebRequest”的实例C:\Users\Dylan\AppData\Local\TemporaryProjects\ConsoleApplication1\Program.cs 18 37 ConsoleApplication1
成功运行了该程序,但没有任何输出:(尝试添加Console.ReadLine();
到程序的末尾。这样,在按enter键之前,您将看到输出。在您有机会查看输出之前,程序将关闭。因此,添加Console.ReadLine();
以便程序等待您按enter键。