/// <summary> /// 在百度知道查询答案。 /// </summary> /// <param name="sentence">要查询的句子</param> /// <param name="num">获取的答案数</param> /// <returns></returns> public string[] getBaiduZhidaoAnswers(string sentence, int num = 10) { List <string> res = new List <string>(); try { string url = $"https://zhidao.baidu.com/search?word={WebConnectActor.UrlEncode(sentence)}"; string html = WebConnectActor.getData(url, Encoding.GetEncoding("gb2312"), cookie); //FileIOActor.log(url); //FileIOActor.log(html); HtmlDocument hdoc = new HtmlDocument(); hdoc.LoadHtml(html); HtmlNode favurl = null; try { var node = hdoc.DocumentNode.SelectSingleNode("//dt[@class=\"dt mb-8\"]"); if (node != null) { favurl = node.ChildNodes[1]; } } catch (Exception ex) { FileIOActor.log(ex); } var urls = hdoc.DocumentNode.SelectNodes("//a[@class=\"ti\"]"); if (favurl != null) { urls.Insert(0, favurl); } foreach (var aurl in urls) { string dw = ItemParser.removeBlank(aurl.GetAttributeValue("href", ""), true); var areslist = getBaiduZhidaoAnswersByUrl(dw); if (areslist.Length > 0) { res.Add(areslist[0].Trim()); } if (res.Count > num) { break; } } } catch (Exception ex) { FileIOActor.log(ex); } return(res.ToArray()); }
/// <summary> /// 从百度知识图谱数据中取得问题的答案 /// 百度知识图谱包括一些常识信息,也能数学运算、查汇率之类的。 /// 和百度搜索结果中的“智能”显示的知识部分一致 /// </summary> /// <param name="str"></param> /// <returns></returns> public string getKGAnswer(string str) { if (string.IsNullOrWhiteSpace(str)) { return(""); } var res = getBaiduKGResult(str); if (res.Length > 0) { return(ItemParser.removeBlank(res[0])); } else { return(""); } }
/// <summary> /// 暂时不可用 /// </summary> /// <param name="question"></param> /// <returns></returns> public string getAsklibResult(string question) { string url = string.Format("http://www.asklib.com/s/{0}", WebConnectActor.UrlEncode(question)); string res = ""; //List<string> res = new List<string>(); string html = WebConnectActor.getData(url, Encoding.UTF8); HtmlDocument hdoc = new HtmlDocument(); hdoc.LoadHtml(html); try { HtmlNode favurl = null; try { //res = html; return res; favurl = hdoc.DocumentNode.SelectSingleNode("//div[@class=\"p15 right\"]").ChildNodes[1]; url = ItemParser.removeBlank(favurl.GetAttributeValue("href", ""), true); url = "http://www.asklib.com/" + url; html = WebConnectActor.getData(url, Encoding.UTF8); hdoc = new HtmlDocument(); hdoc.LoadHtml(html); var tmp = getText(hdoc.DocumentNode.SelectSingleNode("//div[@class=\"listtip\"]").InnerHtml); StringBuilder sb = new StringBuilder(); foreach (var t in tmp) { if (!string.IsNullOrWhiteSpace(t.Trim())) { sb.Append(t + "\r\n"); } } sb.Replace("\r\n\r\n", "\r\n"); res = sb.ToString(); } catch { } } catch { } return(res); }