< query > - .
< sortby > - :
< rlv > - ,
< tm > - .
< maxpassages > - ( - 5, -2).
< groupings > - < groupby > .
< groupby > - , :
< attr = > - , :
< d > - .
<> - .
< mode = > - :
< flat > - .
< deep > - .
< groups-on-page = > - ( 100).
< docs-in-group = > - .
* This source code was highlighted with Source Code Highlighter .
ServicePointManager.Expect100Continue = false ;
/* , IP,
API.*/
string url = @"http://xmlsearch.yandex.ru/xmlsearch?
user=**********&
key=**********************************" ;
// XML
string command =
@"<?xml version=" "1.0" " encoding=" "UTF-8" "?>
<request>
<query>- </query>
<groupings>
<groupby attr=" "d" "
mode=" "deep" "
groups-on-page=" "10" "
docs-in-group=" "1" " />
</groupings>
</request>" ;
byte [] bytes = Encoding .UTF8.GetBytes(command);
// , .
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST" ;
request.ContentLength = bytes.Length;
request.ContentType = "text/xml" ;
// XML-
using ( Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
//
HttpWebResponse response =(HttpWebResponse)request.GetResponse();
* This source code was highlighted with Source Code Highlighter .
//, IP.
string key = "***********************************" ;
// .
string user = "*****************" ;
// .
string url = @"http://xmlsearch.yandex.ru/xmlsearch?
query={0}&
groupby=attr%3Dd.
mode%3Ddeep.
groups-on-page%3D10.
docs-in-group%3D1&
user={1}&
key={2}" ;
// .
string completeUrl = String .Format(url, searchQuery, user, key);
//, .
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(completeUrl);
// .
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
* This source code was highlighted with Source Code Highlighter .
XmlReader xmlReader = XmlReader.Create(response.GetResponseStream());
XDocument xmlResponse = XDocument .Load(xmlReader);
* This source code was highlighted with Source Code Highlighter .
• < url > - URL
• < title > -
• < headline > -
• < modtime > -
• < saved-copy-url > -
* This source code was highlighted with Source Code Highlighter .
public struct YaSearchResult
{
//url
public string DisplayUrl,
//saved-copy-url
CacheUrl,
//title
Title,
//headline
Description,
//modtime
IndexedTime;
public YaSearchResult( string url,
string cacheUrl,
string title,
string description,
string indexedTime)
{
this .DisplayUrl = url;
this .CacheUrl = cacheUrl;
this .Title = title;
this .Description = description;
this .IndexedTime = indexedTime;
}
}
* This source code was highlighted with Source Code Highlighter .
<group>
<categ attr= "" name= "" />
<doccount> </doccount>
<relevance priority= "" />
-<doc id= "" >
<relevance priority= "" />
<url> </url>
<domain> </domain>
<title> </title>
<modtime> </modtime>
<size> </size>
<charset> </charset>
+<passages>
+<properties>
<mime-type> </mime-type>
<saved-copy-url> </saved-copy-url>
</doc>
</group>
* This source code was highlighted with Source Code Highlighter .
// doc,
// GetValue , ,
public static string GetValue( XElement group, string name)
{
try
{
return group.Element(«doc»).Element(name).Value;
}
// ,
// .
catch
{
return string .Empty;
}
}
* This source code was highlighted with Source Code Highlighter .
public static List <YaSearchResult> Search( string searchQuery)
{
// YaSearchResult, .
List <YaSearchResult> ret = new List <YaSearchResult>();
// XML' "group" -
var groupQuery = from gr in response.Elements().
Elements( "response" ).
Elements( "results" ).
Elements( "grouping" ).
Elements( "group" )
select gr;
// group SearchResult
for ( int i = 0; i < groupQuery.Count(); i++)
{
string urlQuery = GetValue(groupQuery.ElementAt(i), "url" );
string titleQuery = GetValue(groupQuery.ElementAt(i), "title" );
string descriptionQuery = GetValue(groupQuery.ElementAt(i), "headline" );
string indexedTimeQuery = GetValue(groupQuery.ElementAt(i), "modtime" );
string cacheUrlQuery = GetValue(groupQuery.ElementAt(i),
"saved-copy-url" );
ret.Add( new YaSearchResult(urlQuery, cacheUrlQuery, titleQuery, descriptionQuery, indexedTimeQuery));
}
return ret;
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/116479/