现在的位置: 首页 > 综合 > 正文

通过C#使用googleAPI (二)2/2

2018年05月14日 ⁄ 综合 ⁄ 共 6406字 ⁄ 字号 评论关闭
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。
 
通过对这个方法的各个参数的设定可以是自己的应用程序在搜索方面更加人性化。具体使用时可以通过各种控件来收集这些信息。
 
(三)
GoogleSearchResult类

这个类用于存储搜索结果,下面介绍这个类的一些重要属性:
1.         estimatedTotalResultsCount 所有与关键字有关的信息的总数。
2.         searchTime 提交信息到返回信息所用的时间。
3.         searchTips 根据关键字提供一些与关键字相关的关键字,供选择。
4.         resultElements 返回一个数组,用于存储返回结果的每一项。
 
Result Element类
这个类的是搜索的结果中的每一项,所有的结果处理都依靠这个类。下面是这个类的一些属性的介绍:
1.         URL 显示结果网页的url。
2.         snippet 返回在目标网页中摘录一些包含关键字片段。
3.         title  目标网页的标题。
4.         hostname 该参数显示目标网页所在的主机明。
5.         summary 如果目标网页在ODP 目录中存在,则返回摘要信息。
6.         directoryTitle 显示ODP 目录的标题。
 
通过这两个类可以取得各种与查询有关的信息,通过以上的介绍,基本可以使用googleAPI提供的全部功能。

 

抱歉!评论已关闭.