我想使用(任何编程语言)C#.NET开发桌面应用程序;要求是在文本视图中显示问题的答案,就像Google在搜索图像中显示的任何问题时所做的那样。
我想从Google搜索中以粗体显示文本,以便我可以将其存储在我的应用中,并在我的应用中向用户显示结果
答案 0 :(得分:2)
它有两种选择:
您需要获取HTML代码,然后对其进行处理以查找所谓“最佳结果”的签名。
您可以使用此示例中的代码来获取HTML代码:
string urlAddress = "https://www.google.co.il/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=what%20is%20the%20weight%20of%20human%20heart";
// need to process to get the real URL of the question.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream,Encoding.GetEncoding(response.CharacterSet));
}
string data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
这将为您提供网站上返回的HTML代码。
要提取最高结果,您可以使用此处讨论的一些HTML解析器:What is the best way to parse html in C#?
您还可以使用Google API:
using Google.API.Search;
然后
var client = new GwebSearchClient("http://www.google.com");
var results = client.Search("google api for .NET", 100);
foreach (var webResult in results)
{
//Console.WriteLine("{0}, {1}, {2}", webResult.Title, webResult.Url, webResult.Content);
listBox1.Items.Add(webResult.ToString ());
}