通过c#窗口应用程序在google中搜索关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18389714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
search keywords in google through c# window application
提问by user2499974
i want to work on a scraper program which will search keyword in google. i have problem in starting my scraper program. my problem is: let suppose window application(c#) have 2 textboxes and a button control. first textbox have "www.google.com" and the 2nd textbox contain keywork for example:
我想开发一个可以在谷歌中搜索关键字的抓取程序。我在启动我的刮刀程序时遇到问题。我的问题是:假设窗口应用程序(c#)有 2 个文本框和一个按钮控件。第一个文本框有“www.google.com”,第二个文本框包含例如:
textbox1: www.google.com textbox2: "cricket"
文本框 1:www.google.com 文本框 2:“板球”
i want code to add to the button click event that will search cricket in google. if anyone have a programing idea in c# then plz help me.
我想要代码添加到将在谷歌搜索板球的按钮点击事件。如果有人在 C# 中有编程想法,请帮助我。
best regards
此致
采纳答案by user2499974
i have googled my problem and found solution to the above problem... we can use google API for this purpose...when we add reference to google api then we will add the following namespace in our program...........
我已经用谷歌搜索了我的问题并找到了上述问题的解决方案......我们可以为此使用谷歌API......当我们添加对谷歌API的引用时,我们将在我们的程序中添加以下命名空间...... ....
using Google.API.Search;
write the following code in button click event
在按钮点击事件中编写以下代码
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 ());
}
test my solution and give comments .........thanx everybody
测试我的解决方案并发表评论........谢谢大家
回答by Matt
I agree with Paqogomez that you don't appear to have put much work into this but I also understand that it can be hard to get started. Here is some sample code that should get you on the right path.
我同意 Paqogomez 的观点,您似乎没有为此付出太多努力,但我也理解这可能很难开始。下面是一些示例代码,可以让您走上正确的道路。
private void button1_Click(object sender, EventArgs e)
{
string uriString = "http://www.google.com/search";
string keywordString = "Test Keyword";
WebClient webClient = new WebClient();
NameValueCollection nameValueCollection = new NameValueCollection();
nameValueCollection.Add("q", keywordString);
webClient.QueryString.Add(nameValueCollection);
textBox1.Text = webClient.DownloadString(uriString);
}
This code will search for "Test Keyword" on Google and return the results as a string.
此代码将在 Google 上搜索“Test Keyword”并将结果作为字符串返回。
The problems with what you are asking is Google is going to return your result as HTML that you will need to parse. I really think you need to do some research on the Google API and what is needed to programmatically request data from Google. Start your search here Google Developers.
您所问的问题是 Google 会将您的结果作为您需要解析的 HTML 返回。我真的认为您需要对 Google API 以及以编程方式从 Google 请求数据所需的内容进行一些研究。在此处开始搜索Google Developers。
Hope this helps get you started on the right path.
希望这有助于您走上正确的道路。