/// <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); }
/// <summary> /// 从百度知道的问答中找回复 /// 提取出多条搜索结果,然后从中随机选一个 /// </summary> /// <param name="str"></param> /// <returns></returns> public string getZhidaoAnswer(string str) { if (string.IsNullOrWhiteSpace(str)) { return(""); } string result = ""; var res1 = getBaiduZhidaoAnswers(str, 5); if (res1.Length > 0) { int maxlen = 1000; int findwidth = 30; var tmp = res1[rand.Next(0, res1.Length)].Replace("展开全部", "").Replace("\r", "").Trim(); tmp = ItemParser.StripHTML(tmp); try { //if (!Directory.Exists(path + answerPath)) Directory.CreateDirectory(path + answerPath); //File.WriteAllText($"{path}{answerPath}{str}.txt", tmp); } catch (Exception e) { FileIOActor.log(e.Message + "\r\n" + e.StackTrace); } if (tmp.Length <= maxlen) { result = tmp; } else { var tmp2 = tmp;//.Split(new char[] { '\n' },StringSplitOptions.RemoveEmptyEntries)[0]; if (tmp2.Length >= maxlen) { int cutPos = tmp2.IndexOfAny(new char[] { '。', '!', '?', '…', '!', '?' }, maxlen - findwidth); if (cutPos > 0 && cutPos < maxlen + findwidth) { result = tmp2.Substring(0, cutPos); } else { cutPos = tmp2.IndexOfAny(new char[] { ',', ';', '、', ',', '.', '》', '”', '"', '\'' }, maxlen - findwidth); if (cutPos > 0 && cutPos < maxlen + findwidth) { result = tmp2.Substring(0, cutPos); } else { result = tmp2.Substring(0, maxlen); } } } else { result = tmp2; } result += "..."; } } return(result); }