通过C#使用googleAPI
- 准备工作
要使用googleAPI 来开发自己的桌面应用程序,先要做下面的准备工作:
1. 首先在下面地址http://www.google.com/apis/download.html下载the Google Web APIs Developer's Kit。
2. 然后在下面地址https://www.google.com/accounts/NewAccount?continue=http://api.google.com/createkey&followup=http://api.google.com/createkey注册一个 license key 以使用google提供的搜索服务。
- 新建一个Google Search应用程序
1. 新建一个Windows From 项目命名为Google Search。
2. 添加Web引用,以便使用google web services。具体做法是将下载的the Google Web APIs Developer's Kit中的GoogleSearch.wsdl文件放入本地的服务器上,然后在VS中右键点击资源管理器中的引用,再选择添加Web引用,然后输入GoogleSearch.wsdl文件在本机服务器上的地址http://localhost/GoogleSearch.wsdl,更改Web引用名为GoogleSearch后,点击添加引用后返回。
3. 进行界面设计,添加3个控件,TextBox(用于输入关键字),Button(用于提交信息),RichTextBox(用于显示搜索结果信息)。
4. 编写事件处理。
1) 添加命名空间。using Google_Search.googlesearch;
2) 双击Button控件,在Button的事件处理中添加下面的代码:
try
{
GoogleSearchService s = new GoogleSearchService(); //建立一个搜索服务。
GoogleSearchResult r = s.doGoogleSearch("NGYfW7dQFHKshnXPwvctLsaipk03YK2x", textBox1.Text, 0, 10, true, "", true, "", "", ""); //使用doGoogleSearch方法取得搜索结果。
ResultElement[] re = r.resultElements; //将结果传入一个ResultElement数组。
foreach (ResultElement n in re)
{
richTextBox1.AppendText(n.title+"\r"); //显示标题。
richTextBox1.AppendText(n.snippet+"\r"); //显示包含关键字摘要信息。
richTextBox1.AppendText(n.URL); //显示网页地址。
richTextBox1.AppendText("\n\r\r");
}
}
{
GoogleSearchService s = new GoogleSearchService(); //建立一个搜索服务。
GoogleSearchResult r = s.doGoogleSearch("NGYfW7dQFHKshnXPwvctLsaipk03YK2x", textBox1.Text, 0, 10, true, "", true, "", "", ""); //使用doGoogleSearch方法取得搜索结果。
ResultElement[] re = r.resultElements; //将结果传入一个ResultElement数组。
foreach (ResultElement n in re)
{
richTextBox1.AppendText(n.title+"\r"); //显示标题。
richTextBox1.AppendText(n.snippet+"\r"); //显示包含关键字摘要信息。
richTextBox1.AppendText(n.URL); //显示网页地址。
richTextBox1.AppendText("\n\r\r");
}
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
{
MessageBox.Show(ee.Message);
}
3) 按ctrl+F5就能测试应用程序了。
-
不足之处
通过以上的的操作,一个简单的应用程序就完成了,但是在显示方面还不是很完美,而且很多属性和方法都没用到,以及一些参数的用法,在下一篇中会提到这些。通过这些可以用到google的强大功能。
GoogleSearchServices类doGoogleSearch方法
需要进行搜索先建立一个GoogleSearchServices 类的对象,然后调用doGoogleSearch方法取得信息。下面是google提供的对该方法的描述。
public doGoogleSearch(string key, string q, int start, int maxResults, bool filter, string restrict, bool safeSearch, string lr, string ie, string oe)
下面对各个参数进行介绍:
Key:这是由google提供的一个认证用的ID,可以在http://www.google.com/apis/申请。由于目前是测试阶段,每个ID一天只提供1000次的搜索服务请求。
q:这个就是要搜索的关键字。string类型
start:结果开始的索引数,从0开始。 int 类型
maxresults:返回的结果数的最大值,最大为10。int 类型
filter:用于标识是否对搜索的结果进行过滤,所进行的过滤是对于同一主机上的内容只返回1—2个结果。bool类型
restrict:用于限定搜索的国家或地区,如果为空,表示不限制。string 类型。
下表将列出所有能选的代号。
国家 |
代号 |
国家 |
代号 |
国家 |
代号 |
国家 |
代号 |
Andorra |
countryAD |
Estonia |
countryEE |
Kazakhstan |
countryKZ |
Qatar |
countryQA |
United Arab Emirates |
countryAE |
Egypt |
countryEG |
Lao People's Democratic Republic |
countryLA |
Reunion |
countryRE |
Afghanistan |
countryAF |
Western Sahara |
countryEH |
Lebanon |
countryLB |
Romania |
countryRO |
Antigua and Barbuda |
countryAG |
Eritrea |
countryER |
Saint Lucia |
countryLC |
Russian Federation |
countryRU |
Anguilla |
countryAI |
Spain |
countryES |
Liechtenstein |
countryLI |
Rwanda |
countryRW |
Albania |
countryAL |
Ethiopia |
countryET |
Sri Lanka |
countryLK |
Saudi Arabia |
countrySA |
Armenia |
countryAM |
European Union |
countryEU |
Liberia |
countryLR |
Solomon Islands |
countrySB |
Netherlands Antilles |
countryAN |
Finland |
countryFI |
Lesotho |
countryLS |
Seychelles |
countrySC |
Angola |
countryAO |
Fiji |
countryFJ |
Lithuania |
countryLT |
Sudan |
countrySD |
Antarctica |
countryAQ |
Falkland Islands (Malvinas) |
countryFK |
Luxembourg |
countryLU |
Sweden |
countrySE |
Argentina |
countryAR |
Micronesia, Federated States of |
countryFM |
Latvia |
countryLV |
Singapore |
countrySG |
American Samoa |
countryAS |
Faroe Islands |
countryFO |
Libyan Arab Jamahiriya |
countryLY |
St. Helena |
countrySH |
Austria |
countryAT |
France |
countryFR |
Morocco |
countryMA |
Slovenia |
countrySI |
Australia |
countryAU |
France, Metropolitan |
countryFX |
Monaco |
countryMC |
Svalbard and Jan Mayen Islands |
countrySJ |
Aruba |
countryAW |
Gabon |
countryGA |
Moldova |
countryMD |
Slovakia (Slovak Republic) |
countrySK |
Azerbaijan |
countryAZ |
United Kingdom |
countryUK |
Madagascar |
countryMG |
Sierra Leone |
countrySL |
Bosnia and Herzegowina |
countryBA |
Grenada |
countryGD |
Marshall Islands |
countryMH |
San Marino |
countrySM |
Barbados |
countryBB |
Georgia |
countryGE |
Macedonia, The Former Yugoslav Republic of |
countryMK |
Senegal |
countrySN |
Bangladesh |
countryBD |
French Quiana |
countryGF |
Mali |
countryML |
Somalia |
countrySO |
Belgium |
countryBE |
Ghana |
countryGH |
Myanmar |
countryMM |
Suriname |
countrySR |
Burkina Faso |
countryBF |
Gibraltar |
countryGI |
Mongolia |
countryMN |
Sao Tome and Principe |
countryST |
Bulgaria |
countryBG |
Greenland |
countryGL |
Macau |
countryMO |
El Salvador |
countrySV |
Bahrain |
countryBH |
Gambia |
countryGM |
Northern Mariana Islands |
countryMP |
Syria |
countrySY |
Burundi |
countryBI |
Guinea |
countryGN |
Martinique |
countryMQ |
Swaziland |
countrySZ |
Benin |
countryBJ |
Guadeloupe |
countryGP |
Mauritania |
countryMR |
Turks and Caicos Islands |
countryTC |
Bermuda |
countryBM |
Equatorial Guinea |
countryGQ |
Montserrat |
countryMS |
Chad |
countryTD |
Brunei Darussalam |
countryBN |
Greece |
countryGR |
Malta |
countryMT |
French Southern Territories |
countryTF |
Bolivia |
countryBO |
South Georgia and the South Sandwich Islands |
countryGS |
Mauritius |
countryMU |
Togo |
countryTG |
Brazil |
countryBR |
Guatemala |
countryGT |
Maldives |
countryMV |
Thailand |
countryTH |
Bahamas |
countryBS |
Guam |
countryGU |
Malawi |
countryMW |
Tajikistan |
countryTJ |
Bhutan |
countryBT |
Guinea-Bissau |
countryGW |
Mexico |
countryMX |
Tokelau |
countryTK |
Bouvet Island |
countryBV |
Guyana |
countryGY |
Malaysia |
countryMY |
Turkmenistan |
countryTM |
Botswana |
countryBW |
Hong Kong |
countryHK |
Mozambique |
countryMZ |
Tunisia |
countryTN |
Belarus |
countryBY |
Heard and Mc Donald Islands |
countryHM |
Namibia |
countryNA |
Tonga |
countryTO |
Belize |
countryBZ |
Honduras |
countryHN |
New Caledonia |
countryNC |
East Timor |
countryTP |
Canada |
countryCA |
Croatia (local name: Hrvatska) |
countryHR |
Niger |
countryNE |
Turkey |
countryTR |
Cocos (Keeling) Islands |
countryCC |
Haiti |
countryHT |
Norfolk Island |
countryNF |
Trinidad and Tobago |
countryTT |
Congo, The Democratic Republic of the |
countryCD |
Hungary |
countryHU |
Nigeria |
countryNG |
Tuvalu |
countryTV |
Central African Republic |
countryCF |
Indonesia |
countryID |
Nicaragua |
countryNI |
Taiwan |
countryTW |
Congo |
countryCG |
Ireland |
countryIE |
Netherlands |
countryNL |
Tanzania |
countryTZ |
Switzerland |
countryCH |
Israel |
countryIL |
Norway |
countryNO |
Ukraine |
countryUA |
Cote D'ivoire |
countryCI |
India |
countryIN |
Nepal |
countryNP |
Uganda |
countryUG |
Cook Islands |
countryCK |
British Indian Ocean Territory |
countryIO |
Nauru |
countryNR |
United States Minor Outlying Islands |
countryUM |
Chile |
countryCL |
Iraq |
countryIQ |
Niue |
countryNU |
United States |
countryUS |
Cameroon |
countryCM |
Iran (Islamic Republic of) |
countryIR |
New Zealand |
countryNZ |
Uruguay |
countryUY |
China |
countryCN |
Iceland |
countryIS |
Oman |
countryOM |
Uzbekistan |
countryUZ |
Colombia |
countryCO |
Italy |
countryIT |
Panama |
countryPA |
Holy See (Vatican City State) |
countryVA |
Costa Rica |
countryCR |
Jamaica |
countryJM |
Peru |
countryPE |
Saint Vincent and the Grenadines |
countryVC |
Cuba |
countryCU |
Jordan |
countryJO |
French Polynesia |
countryPF |
Venezuela |
countryVE |
Cape Verde |
countryCV |
Japan |
countryJP |
Papua New Guinea |
countryPG |
Virgin Islands (British) |
countryVG |
Christmas Island |
countryCX |
Kenya |
countryKE |
Philippines |
countryPH |
Virgin Islands (U.S.) |
countryVI |
Cyprus |
countryCY |
Kyrgyzstan |
countryKG |
Pakistan |
countryPK |
Vietnam |
countryVN |
Czech Republic |
countryCZ |
Cambodia |
countryKH |
Poland |
countryPL |
Vanuatu |
countryVU |
Germany |
countryDE |
Kiribati |
countryKI |
St. Pierre and Miquelon |
countryPM |
Wallis and Futuna Islands |
countryWF |
Djibouti |
countryDJ |
Comoros |
countryKM |
Pitcairn |
countryPN |
Samoa |
countryWS |
Denmark |
countryDK |
Saint Kitts and Nevis |
countryKN |
Puerto Rico |
countryPR |
Yemen |
countryYE |
Dominica |
countryDM |
Korea, Democratic People's Republic of |
countryKP |
Palestine |
countryPS |
Mayotte |
countryYT |
Dominican Republic |
countryDO |
Korea, Republic of |
countryKR |
Portugal |
countryPT |
Yugoslavia |
countryYU |
Algeria |
countryDZ |
Kuwait |
countryKW |
Palau |
countryPW |
South Africa |
countryZA |
Ecuador |
countryEC |
Cayman Islands |
countryKY |
Paraguay |
countryPY |
Zambia |
countryZM |
|
|
|
|
|
|
Zaire |
countryZR |
safeSearch:使用一个bool值表示是否过滤垃圾信息。
lr:对语言的限制,string类型。可选代号如下表:
语言 |
代号 |
|
语言 |
代号 |
Arabic |
lang_ar |
|
Icelandic |
lang_is |
Chinese (S) |
lang_zh-CN |
|
Italian |
lang_it |
Chinese (T) |
lang_zh-TW |
|
Japanese |
lang_ja |
Czech |
lang_cs |
|
Korean |
lang_ko |
Danish |
lang_da |
|
Latvian |
lang_lv |
Dutch |
lang_nl |
|
Lithuanian |
lang_lt |
English |
lang_en |
|
Norwegian |
lang_no |
Estonian |
lang_et |
|
Portuguese |
lang_pt |
Finnish |
lang_fi |
|
Polish |
lang_pl |
French |
lang_fr |
|
Romanian |
lang_ro |
German |
lang_de |
|
Russian |
lang_ru |
Greek |
lang_el |
|
Spanish |
lang_es |
Hebrew |
lang_iw |
|
Swedish |
lang_sv |
Hungarian |
lang_hu |
|
Turkish |
lang_tr |
ie:输入所使用的字符编码。这个参数将被忽略,所有的输入都将采用UTF-8。
oe:输出所使用的字符编码。这个参数将被忽略,所有的输出都将采用UTF-8
。
通过对这个方法的各个参数的设定可以是自己的应用程序在搜索方面更加人性化。具体使用时可以通过各种控件来收集这些信息。
l GoogleSearchResult类
这个类用于存储搜索结果,下面介绍这个类的一些重要属性:
1. estimatedTotalResultsCount 所有与关键字有关的信息的总数。
2. searchTime 提交信息到返回信息所用的时间。
3. searchTips 根据关键字提供一些与关键字相关的关键字,供选择。
4. resultElements 返回一个数组,用于存储返回结果的每一项。
l Result Element类
这个类的是搜索的结果中的每一项,所有的结果处理都依靠这个类。下面是这个类的一些属性的介绍:
1. URL 显示结果网页的url。
2. snippet 返回在目标网页中摘录一些包含关键字片段。
3. title 目标网页的标题。
4. hostname 该参数显示目标网页所在的主机明。
5. summary 如果目标网页在ODP 目录中存在,则返回摘要信息。
6. directoryTitle 显示ODP 目录的标题。
通过这两个类可以取得各种与查询有关的信息,通过以上的介绍,基本可以使用googleAPI提供的全部功能。