コード例 #1
0
        public Definition Search(string word)
        {
            var Definition = new Definition(word);

            var          query = builder.GetQuery(word);
            var          url   = BaseAddress + query;
            HtmlDocument doc   = null;

            try
            {
                doc = Client.Load(url);
            }
            catch { }
            if (doc != null)
            {
                //doc.OptionOutputAsXml = true;
                var result  = doc.DocumentNode.OuterHtml;
                var formDiv = (HtmlNode)doc.QuerySelector(".form");
                if (formDiv != null)
                {
                    var l = new List <HtmlNode>();
                    foreach (HtmlNode child in formDiv.ChildNodes)
                    {
                        if (child.Name != "h3" && child.Name != "ul")
                        {
                            l.Add(child);
                        }
                    }
                    foreach (HtmlNode childNode in l)
                    {
                        formDiv.RemoveChild(childNode, false);
                    }



                    var type = "";
                    foreach (HtmlNode child in formDiv.ChildNodes)
                    {
                        if (child.Name == "h3")
                        {
                            type = child.InnerText;
                            Definition.AddRootType(type);
                        }
                        else
                        {
                            var listItems = child.QuerySelectorAll("li");
                            foreach (HtmlNode li in listItems)
                            {
                                var liChildren = li.ChildNodes;

                                if (liChildren.Count > 0)
                                {
                                    try
                                    {
                                        var typeNode = liChildren.First(x => x.Name == "#text");
                                        var posType  = typeNode.InnerText;
                                        posType = posType.Substring(posType.IndexOf(";") + 1);
                                        posType = posType.Substring(0, posType.IndexOf("&"));

                                        var posIdx   = posType.IndexOf('.');
                                        var rootType = posType.Substring(0, posIdx);
                                        var subType  = posType.Substring(posIdx + 1);

                                        var bNode = liChildren.FirstOrDefault(x => x.Name == "b");
                                        var index = "1";
                                        if (bNode != null)
                                        {
                                            var bText     = bNode.InnerText.Trim();
                                            var sFullName = bText.Substring(0, bText.IndexOf(" "));
                                            index = sFullName.Substring(sFullName.IndexOf('#') + 1);
                                        }
                                        var defNode    = liChildren.Last(x => x.Name == "#text");
                                        var definition = defNode.InnerText.Trim().Substring(1).TrimEnd(')');



                                        var dto = new EntryDto(index, rootType, subType, definition);
                                        Definition.AddEntry(dto);
                                    }
                                    catch (Exception ex)
                                    {
                                        string bp = ex.Message;
                                        Console.WriteLine($"Error parsing definition: {ex.Message}");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(Definition);
        }