iciba.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. namespace TxtDictionary
  7. {
  8. public class iciba
  9. {
  10. WebClient wc = new WebClient();
  11. string urlBase = "http://wap.iciba.com/cword/";
  12. public Word QueryWord(string wordText)
  13. {
  14. var data = wc.DownloadData(urlBase + wordText);
  15. var htmlStr = Encoding.UTF8.GetString(data);
  16. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  17. doc.LoadHtml(htmlStr);
  18. var nodes = doc.DocumentNode.SelectNodes("//div[@class='h2']");
  19. Word wordToReturn = null;
  20. if (nodes!=null && nodes.Count >= 2)
  21. {
  22. List<Meaning> lm = new List<Meaning>();
  23. for (int i = 2; i <= nodes.Count; i++)
  24. {
  25. //只有一个意思的时候 i =2;
  26. Console.WriteLine(i.ToString());
  27. string phoneticEN,phoneticUS;
  28. if (i == 2)
  29. {
  30. try
  31. {
  32. phoneticEN = nodes[i - 1].ChildNodes[3].ChildNodes["span"].InnerText;
  33. }
  34. catch
  35. {
  36. phoneticEN = "";
  37. }
  38. try
  39. {
  40. phoneticUS = nodes[i - 1].ChildNodes[4].ChildNodes["span"].InnerText;
  41. }
  42. catch
  43. {
  44. phoneticUS = "";
  45. }
  46. }
  47. else
  48. {
  49. try
  50. {
  51. phoneticEN = nodes[i - 1].ChildNodes[1].ChildNodes["span"].InnerText;
  52. }
  53. catch
  54. {
  55. phoneticEN = "";
  56. }
  57. try
  58. {
  59. phoneticUS = nodes[i - 1].ChildNodes[2].ChildNodes["span"].InnerText;
  60. }
  61. catch
  62. {
  63. phoneticUS = "";
  64. }
  65. }
  66. var ns = doc.DocumentNode.SelectNodes("//div[@class='y']");
  67. string sub1 = ns[i - 2].InnerHtml;
  68. HtmlAgilityPack.HtmlDocument docSub = new HtmlAgilityPack.HtmlDocument();
  69. docSub.LoadHtml(sub1);
  70. var nodesSub = docSub.DocumentNode.SelectNodes("dl");
  71. List<CixingChinese> lcc = new List<CixingChinese>();
  72. foreach (HtmlAgilityPack.HtmlNode item in nodesSub)
  73. {
  74. string cixing = item.ChildNodes["dt"].InnerText;
  75. string yisi = item.ChildNodes["dd"].InnerText;
  76. CixingChinese cc = new CixingChinese()
  77. {
  78. Chinese = yisi,
  79. Cixing = cixing
  80. };
  81. lcc.Add(cc);
  82. }
  83. Meaning m = new Meaning(phoneticEN, phoneticUS, lcc);
  84. lm.Add(m);
  85. }
  86. wordToReturn = new Word(wordText, lm);
  87. }
  88. return wordToReturn;
  89. }
  90. }
  91. }