Answer the question
In order to leave comments, you need to log in
How to automatically get the transcription of an English word from Yandex Translator?
Hello.
I want to automatically receive transcriptions of English words from Yandex Translator. Since I did not find the ability to receive transcriptions in addition to a simple translation in the Yandex Translator API, I decided to "independently" receive transcriptions by parsing the html code on the Yandex Translator web page. To do this, I wrote the following console application in C#:
string sURL = "https://translate.yandex.ru/?text=" + "beware" + "&lang=en-ru";
//string html;
//HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sURL);
//HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
//using (StreamReader stream = new StreamReader(
// resp.GetResponseStream()))
//{
// html = stream.ReadToEnd();
//}
WebClient client = new WebClient();
var data = client.DownloadData(sURL);
var html = Encoding.UTF8.GetString(data);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var x = doc.DocumentNode;
Console.WriteLine("После этой строки появляется ошибка" + x); // В консоли заместо 'x' будет выведено HtmlAgillityPack.HtmlNode
//var y = x.SelectNodes("//span[@class='dictionary-transcription']");
var y = x.SelectNodes("id('dictionaryContent')/x:li[1]/x:div/x:span[1]"); //XPATH-селектор мне автоматически сгенерировал браузер Firefox
var z = y.ToList();
string Text = "Пусто";
foreach (HtmlNode node in z)
{
Text = node.InnerText;
}
Console.WriteLine(Text);
Console.ReadLine();
var y = x.SelectNodes("некий XPATH-селектор");
using System.Xml.Xsl;
<span class="dictionary-transcription">[{{transcription}}]</span>
<span></span>
Answer the question
In order to leave comments, you need to log in
I immediately warn you: you must understand that you are violating the rules for using Yandex services and in no case should you use such solutions for commercial purposes. I hope for your discretion.
Regarding the solution: it is better to enter from the side of the internal API used on the site.
Here is the simplest solution. For parsing Json, the Json.Net library is used (you can rewrite it for parsing with the standard .Net library, it was just faster for me to write an example).
const string text = "dog";
var translateUrl = "https://dictionary.yandex.net/dicservice.json/lookup?ui=ru&text="
+ text + "&lang=en-ru&flags=23";
using (var wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
var resultHtml = wc.DownloadString(translateUrl);
dynamic trsJson = JObject.Parse(resultHtml);
var trs = trsJson.def[0].ts;
Console.WriteLine("Транскрипция: " + trs);
}
The Yandex API Documentation has an explanation of where the transcription lies.
Table 1. Response XML schema elements DicResult
head Result header (not used).
def Array of dictionary entries. The ts attribute may contain the transcription of the searched word.
tr An array of translations.
syn Array of synonyms.
mean An array of values.
ex An array of examples.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question