private double getRate(string cur) { string strUrl, strData; double dbRate; dbRate = 1; if (cur == "USD") { strUrl = "https://rate.bot.com.tw/xrt?Lang=zh-TW"; strData = SingletonClass.singletonClass.RequestUrl(strUrl, System.Text.Encoding.UTF8); NSoup.Nodes.Document htmlDoc = NSoup.NSoupClient.Parse(strData); NSoup.Select.Elements ele = htmlDoc.GetElementsByTag("td"); for (int i = 0; i < ele.Count; i++) { if ((ele[i].Dataset.ContainsKey("data-table")) && (ele[i].Dataset["data-table"] == "本行即期買入")) { dbRate = SingletonClass.singletonClass.ConvertStringToDouble(ele[i].Text().Trim()); break; } } } return(dbRate); }
static void Main(string[] args) { WebClient client = new WebClient(); client.Encoding = System.Text.Encoding.UTF8; var html = client.DownloadString("https://blog.csdn.net/czjnoe/article/details/106600070"); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html); //根据标签名获取节点 NSoup.Select.Elements metaElements = doc.GetElementsByTag("meta"); foreach (var item in metaElements) { } //根据id获取节点 NSoup.Nodes.Element headClassElements = doc.GetElementById("head"); //根据class获取节点 var headIdElements = doc.GetElementsByClass("fm").ToList(); foreach (var item in headIdElements) { } //根据属性名称获取节点 List <Element> attributeNameElements = doc.GetElementsByAttribute("class").ToList(); //根据属性值获取节点 List <Element> attributeValueElements = doc.GetElementsByAttributeValue("id", "su").ToList(); //根据jQuery选择器获取节点 var selectElments = doc.Select("#head").ToList(); }
private NSoup.Select.Elements GetLinks(string url) { WSH.Options.Common.Result result = this.Request(url); if (result.IsSuccess) { string htmlString = result.Msg; NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(htmlString); NSoup.Select.Elements links = doc.GetElementsByTag("a"); NSoup.Select.Elements h3s = doc.GetElementsByTag("h3"); NSoup.Select.Elements spans = doc.GetElementsByTag("span"); if (links != null) { if (h3s != null) { links.AddRange(h3s); } if (spans != null) { links.AddRange(spans); } } return(links); } else { throw new Exception(result.Msg); } }
public static List <ToDoTask> getToDoTasks(string html) { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html); NSoup.Select.Elements ele = doc.GetElementsByTag("span"); List <ToDoTask> result = new List <ToDoTask>(); foreach (var i in ele) { if (i.Attr("style") == "padding:1px 0 0 2px") { var title = i.GetElementsByTag("a")[0]; string content = title.Attr("title"); if (content.Equals("")) { content = title.Text(); } var urgencyElement = title.GetElementsByTag("b")[0]; string urgency = urgencyElement.Html(); var nextSpan = i.NextElementSibling; string time = nextSpan.Html(); ToDoTask toDoTask = new ToDoTask(urgency, content, time); result.Add(toDoTask); } } return(result); }
public static NSoup.Nodes.Element GetElementFirst(NSoup.Select.Elements els) { if (els == null || els.Count <= 0) { return(null); } return(els[0]); }
public static 列表 <NSoup.Nodes.Element> Select(NSoup.Nodes.Element element, string cssQuery) { NSoup.Select.Elements elements = element.Select(cssQuery); NSoup.Nodes.Element[] earray = elements.ToArray(); 列表 <NSoup.Nodes.Element> list = new 列表 <NSoup.Nodes.Element>(earray); return(list); }
public List <String> FindImages(String question, String userAgent) { List <String> imagesList = new List <String>(); try { String googleUrl = "https://www.google.com/search?tbm=isch&q=" + question.Replace(",", ""); NSoup.Nodes.Document htmlDoc = NSoupClient.Connect(googleUrl).UserAgent(userAgent).Timeout(10 * 1000).Get(); //Handling correctly auto redirects... checkForRedirectsOnHTMLDocument(ref htmlDoc, userAgent); /* * //This is old method * NSoup.Select.Elements images = htmlDoc.Select("div.rg_di.rg_el.ivg-i img"); //div with class="rg_di rg_el ivg-i" containing img * foreach (NSoup.Nodes.Element img in images) { * NSoup.Select.Elements links = img.Parent.Select("a[href]"); * if (links.Count() > 0) { //is there a link around img? * NSoup.Nodes.Element link = img.Parent.Select("a[href]").First(); * String href = img.Parent.Attr("abs:href"); //link which needs to be parsed to get the full img url * Regex regex = new Regex("imgurl=(.*?)&imgrefurl="); //Everything between "imgurl=" and "&imgrefurl=" * var v = regex.Match(href); * if (v != null && v.Groups.Count == 2) { * if (v.Groups[1].Value != String.Empty) { * String imgURL = v.Groups[1].ToString(); * imagesList.Add(imgURL); * } * } * } * } */ NSoup.Select.Elements div_with_images = htmlDoc.Select("div.y.yi div.rg_di.rg_bx.rg_el.ivg-i"); //div with class="y yi" containing div with class="rg_di rg_bx rg_el ivg-i" foreach (NSoup.Nodes.Element div_with_image in div_with_images) { NSoup.Nodes.Element rg_meta_div = div_with_image.Select("div.rg_meta").First(); String text_where_the_img_is = rg_meta_div.ToString(); Regex regex = new Regex("ou":"(.*?)""); //Everything between "ou":"" and """ var v = regex.Match(text_where_the_img_is); if (v != null && v.Groups.Count == 2) { if (v.Groups[1].Value != String.Empty) { String imgURL = v.Groups[1].ToString(); imagesList.Add(imgURL); } } } } catch (Exception ex) { this.Error = ex; } return(imagesList); }
/// <summary> /// 解析主页的访问用户 /// </summary> /// <returns></returns> public void VisitParser() { this.Login(); Result result = this.Request(this.HomePageUrl); string pageHtml = result.Msg; if (string.IsNullOrWhiteSpace(pageHtml)) { return; } NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(pageHtml); NSoup.Nodes.Element wrapElement = doc.GetElementById("show_style_01"); NSoup.Select.Elements userElementNodes = wrapElement.GetElementsByTag("li"); if (userElementNodes != null && userElementNodes.Count > 0) { //倒序排列,最新的在最后面 IEnumerable <NSoup.Nodes.Element> userElements = userElementNodes.Reverse(); foreach (NSoup.Nodes.Element userElement in userElements) { NSoup.Nodes.Element picElement = GetElementFirst(userElement.GetElementsByClass("pic")); NSoup.Nodes.Element nameElement = GetElementFirst(userElement.GetElementsByClass("user_name")); NSoup.Nodes.Element userInfoElement = GetElementFirst(userElement.GetElementsByClass("user_info")); NSoup.Nodes.Element dateElement = GetElementFirst(userElement.GetElementsByClass("date")); string userName = nameElement == null ? "" : nameElement.Child(0).Text(); string homePage = UriHelper.RemoveParams(nameElement == null ? "" : nameElement.Child(0).Attr("href")); string pic = picElement.Child(0).Child(0).Attr("src"); DateTime date = Convert.ToDateTime(dateElement.Text().Replace("到访:", "")); string[] userInfo = StringHelper.SplitWhiteSpace(userInfoElement.Child(0).Text()); int age = Convert.ToInt32(userInfo[0].Replace("岁", "")); string addr = userInfo.Length > 1 ? userInfo[1] : string.Empty; string userCode = homePage.Substring(homePage.LastIndexOf('/') + 1); if (addr.Contains("广州") && !string.IsNullOrWhiteSpace(userCode)) { FateUserInfo user = FateUserInfoManager.GetUser(userCode); if (user == null) { user = new FateUserInfo() { CreateTime = DateTime.Now }; } user.ModifyTime = DateTime.Now; user.UserCode = userCode; user.Address = addr; user.Age = age; user.HeadFileName = pic; user.UserName = userName; FateUserInfoManager.SaveOrUpdateUser(user); } } } }
private void RequestData(FundValue fundValue) { //https://www.fundrich.com.tw/fund/116011.html?id=116011#%E5%9F%BA%E9%87%91%E7%B8%BD%E8%A6%BD //https://www.fundrich.com.tw/fund/062003.html?id=062003#%E5%9F%BA%E9%87%91%E7%B8%BD%E8%A6%BD //https://www.fundrich.com.tw/fund/CIT004.html?id=CIT004#%E5%9F%BA%E9%87%91%E7%B8%BD%E8%A6%BD /* * 庫存 * 淨值 * 匯率 * 69.189 * 35.91 * 30.8825 = 76730 * * => 利潤: 76730 / 75000 = 102.3% * */ try { string strPath, strData; strPath = string.Format("{0}\\..\\..\\..\\WindowsFormsApplication1\\Data\\MyData.json", System.AppDomain.CurrentDomain.BaseDirectory); strData = SingletonClass.singletonClass.RequestUrl(fundValue.strUrl, System.Text.Encoding.UTF8); NSoup.Nodes.Document htmlDoc = NSoup.NSoupClient.Parse(strData); NSoup.Select.Elements ele = htmlDoc.GetElementsByTag("span"); for (int i = 0; i < ele.Count; i++) { if ((ele[i].Dataset.ContainsKey("reactid")) && (ele[i].Dataset["reactid"] == "491")) { fundValue.netValue = SingletonClass.singletonClass.ConvertStringToDouble(ele[i].Text().Trim()); } if ((ele[i].Dataset.ContainsKey("reactid")) && (ele[i].Dataset["reactid"] == "492")) { fundValue.cur = ele[i].Text().Trim(); } } fundValue.rate = getRate(fundValue.cur); } catch (Exception ex) { SingletonClass.singletonClass.WriteLog(ex.ToString()); } }
/// <summary> /// 传入整个页面的html,返回只有任务列表那部分html /// 即<ul></ul>之间的html /// 并把所有点击的链接都去除 /// </summary> /// <param name="html"></param> /// <returns></returns> public static string getToDoTaskHtmlUl(string html) { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html); NSoup.Select.Elements ele = doc.GetElementsByClass("con_left"); if (ele.IsEmpty) { NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error("无法解析html:items_ul"); return(null); } ele[0].GetElementsByTag("a").RemoveAttr("href");//把所有点击的链接都去除 StringBuilder sb = new StringBuilder("<div class=\"con_left\">"); sb.Append(ele[0].Html()); sb.Append("</div>"); string ulHtml = sb.ToString(); return(ulHtml); }
private void button3_Click(object sender, EventArgs e) { PublicValue.arrText.Clear(); #region q new Thread(() => { for (int k = 1; k <= 1; k++) { string Url = "https://www.xicidaili.com/nn/" + k; try { string strHtml = Utils.GetHtml(Url); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(strHtml); NSoup.Select.Elements tableEle = doc.GetElementsByTag("table"); foreach (var tableItem in tableEle) { if (tableItem.Id == "ip_list") { NSoup.Select.Elements trEle = tableItem.GetElementsByTag("tr"); foreach (var trItem in trEle) { NSoup.Select.Elements tdEle = trItem.GetElementsByTag("td"); if (tdEle.Count > 3) { string ip = tdEle[1].Text(); int port = 0; string portStr = tdEle[2].Text(); int.TryParse(portStr, out port); PublicValue.arrText.Add(ip + ":" + port); listBox1.Items.Add(ip + ":" + port); } } } } } catch (Exception ex) { MessageBox.Show(ex.Message, TitleInfo, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }).Start(); #endregion }
/// <summary> /// 获取页面验证之后的链接集合 /// </summary> /// <returns></returns> private List <string> GetValidPageLinkAddress() { NSoup.Select.Elements links = GetLinks(this.Url); List <string> hrefs = new List <string>(); if (links != null && links.Count > 0) { if (links.Count == 1 && links[0].TagName().ToLower() == "a") { //需要点击验证 var website = UriHelper.GetUrlRoot(this.Url); string url = UriHelper.Combine(website, GetHref(links[0])); links = GetLinks(url); } if (links != null) { hrefs = GetLinkAddress(links); } } return(hrefs); }
private List <string> GetLinkAddress(NSoup.Select.Elements links) { List <string> linkList = new List <string>(); foreach (NSoup.Nodes.Element link in links) { //过滤重复的链接 switch (link.TagName().ToLower()) { case "span": linkList.AddRange(GetSpanContentLink(link)); break; case "h3": linkList.Add(GetContentLink(link)); break; default: linkList.Add(GetHref(link)); break; } } return(linkList); }
public void TVAdmin_002_DisplayQAFuncForVideo() { try { log.Info("TVAdmin_002_DisplayQAFuncForVideo Test Started" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); objAdminVideoManagement = new Chrome_VideoManagement(driver, log, executor, iWait); objAdminPollManagement = new Chrome_PollManagement(driver, log, executor, iWait); objWebPollManagement = new Chrome_PollManagementVerification(driver, log, executor, iWait); objWebQAManagement = new Chrome_QAManagementVerification(driver, log, executor, iWait); #region Create Live Free Video With QA enable log.Info("\nCreate Live Free Video With QA enable" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); //String videoName = cf.readingXMLFile("AdminPortal", "QAManagement", "videoName", "Config.xml"); String videoName = CreateFreeLiveVideoWithQA(); #endregion #region Search Video on Admin-Video Management and Uncheck the Display QA log.Info("Search Video on Admin-Video Management and Uncheck the Display QA" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); //Redirect to Video Management objAdminPollManagement.RedirectToVideoManagement(); // Search the Video objAdminPollManagement.txtSearchVideoManagement().SendKeys(videoName); objAdminPollManagement.btnSearchVideoManagement().Click(); iWait.Until(ExpectedConditions.InvisibilityOfElementLocated((OR.GetElement("SeriesManagement", "OverlayWait", "TVAdminPortalOR.xml")))); iWait.Until(ExpectedConditions.ElementIsVisible((OR.GetElement("QnAManagement", "VideoListSearch", "TVAdminPortalOR.xml")))); //Thread.Sleep(1000); #region Select Required Video from search result //Using Nsoup here to parse the html table Document doc = NSoup.NSoupClient.Parse(driver.PageSource); NSoup.Select.Elements rowListNsoup = doc.GetElementById(OR.readingXMLFile("QnAManagement", "VideoListSearch", "TVAdminPortalOR.xml")).GetElementsByTag("tr"); int rowCounter = 0; Boolean flag = false; String videoTitle = null; foreach (Element currentRow in rowListNsoup) { Attributes attr = currentRow.Attributes; //Row that have class="GridRowStyle" or class="AltGridStyle" if (attr["class"].Equals("GridRowStyle") || attr["class"].Equals("AltGridStyle")) { log.Info("Row Counter :: " + rowCounter); videoTitle = driver.FindElement(OR.GetElement("QnAManagement", "VideoListResult", "TVAdminPortalOR.xml", rowCounter)).GetAttribute("title").Trim(); if (videoTitle.ToLower().Equals(videoName.ToLower())) { //Clicknig on Edit button driver.FindElement(OR.GetElement("QnAManagement", "VideoEditButton", "TVAdminPortalOR.xml", rowCounter)).Click(); uf.isJqueryActive(driver); } rowCounter++; } } #endregion // Uncheck the Display QnA under Advance objAdminVideoManagement.advanceTab().Click(); objAdminVideoManagement.tabPermission().Click(); if (IsElementChecked()) { objAdminVideoManagement.chkDisplayQA().Click(); } objAdminVideoManagement.publishTab().Click(); objAdminVideoManagement.videoPublishButton().Click(); iWait.Until(ExpectedConditions.ElementIsVisible((OR.GetElement("VideoManagement", "SuccessBTN", "TVAdminPortalOR.xml")))); driver.FindElement((OR.GetElement("VideoManagement", "SuccessBTN", "TVAdminPortalOR.xml"))).Click(); iWait.Until(ExpectedConditions.InvisibilityOfElementLocated((OR.GetElement("VideoManagement", "SuccessBTN", "TVAdminPortalOR.xml")))); #endregion #region Open a new Tab and redirect to Web Portal log.Info("Open a new Tab and redirect to Web Portal" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); uf.OpenNewTab(driver); log.Info("count ::: " + driver.WindowHandles.Count); String browsertype = uf.getRunningBrowser(driver, driver.FindElement(By.TagName("html"))); uf.SwitchToWebTab(driver, browsertype); uf.NavigateWebPortal(cf, driver); uf.isJqueryActive(driver); iWait.Until(ExpectedConditions.InvisibilityOfElementLocated((OR.GetElement("SeriesManagement", "OverlayWait", "TVAdminPortalOR.xml")))); #endregion #region Search video and Verify the presence of QA Panel on Web Portal log.Info("Search video and Verify the presence of QA Panel on Web Portal" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); // Search the video on Web Portal objWebPollManagement.Search(videoName, null, true); uf.isJqueryActive(driver); objWebPollManagement.HandlingEmergencyMessage(); objWebPollManagement.ClickOnVideo(videoName); uf.isJqueryActive(driver); // Verify that poll panel is displayed Assert.AreEqual(false, objWebQAManagement.IsQAPanelDisplayed()); #endregion #region Switch to Admin Portal and Uncheck the Display QA log.Info("Switch to Admin Portal and check the Display QA" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); uf.SwitchToAdminTab(driver, browsertype); // Search the Video objAdminPollManagement.txtSearchVideoManagement().SendKeys(videoName); objAdminPollManagement.btnSearchVideoManagement().Click(); iWait.Until(ExpectedConditions.InvisibilityOfElementLocated((OR.GetElement("SeriesManagement", "OverlayWait", "TVAdminPortalOR.xml")))); iWait.Until(ExpectedConditions.ElementIsVisible((OR.GetElement("QnAManagement", "VideoListSearch", "TVAdminPortalOR.xml")))); //Thread.Sleep(1000); #region Select Required Video from search result //Using Nsoup here to parse the html table doc = NSoup.NSoupClient.Parse(driver.PageSource); rowListNsoup = doc.GetElementById(OR.readingXMLFile("QnAManagement", "VideoListSearch", "TVAdminPortalOR.xml")).GetElementsByTag("tr"); rowCounter = 0; flag = false; videoTitle = null; foreach (Element currentRow in rowListNsoup) { Attributes attr = currentRow.Attributes; //Row that have class="GridRowStyle" or class="AltGridStyle" if (attr["class"].Equals("GridRowStyle") || attr["class"].Equals("AltGridStyle")) { log.Info("Row Counter :: " + rowCounter); videoTitle = driver.FindElement(OR.GetElement("QnAManagement", "VideoListResult", "TVAdminPortalOR.xml", rowCounter)).GetAttribute("title").Trim(); if (videoTitle.ToLower().Equals(videoName.ToLower())) { //Clicknig on Edit button driver.FindElement(OR.GetElement("QnAManagement", "VideoEditButton", "TVAdminPortalOR.xml", rowCounter)).Click(); uf.isJqueryActive(driver); } rowCounter++; } } #endregion // Uncheck the Display Polling under Advance objAdminVideoManagement.advanceTab().Click(); objAdminVideoManagement.tabPermission().Click(); objAdminVideoManagement.chkDisplayQA().Click(); objAdminVideoManagement.publishTab().Click(); objAdminVideoManagement.videoPublishButton().Click(); iWait.Until(ExpectedConditions.ElementIsVisible((OR.GetElement("VideoManagement", "SuccessBTN", "TVAdminPortalOR.xml")))); driver.FindElement((OR.GetElement("VideoManagement", "SuccessBTN", "TVAdminPortalOR.xml"))).Click(); iWait.Until(ExpectedConditions.InvisibilityOfElementLocated((OR.GetElement("VideoManagement", "SuccessBTN", "TVAdminPortalOR.xml")))); #endregion #region Switch to Web Portal and verify the QA panel log.Info("Switch to Web Portal and verify the QA panel" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); uf.SwitchToWebTab(driver, browsertype); driver.Navigate().Refresh(); uf.isJqueryActive(driver); // Verify that QnA panel is displayed Assert.AreEqual(true, objWebQAManagement.IsQAPanelDisplayed()); #endregion log.Info("\nTVAdmin_002_DisplayQAFuncForVideo Test Completed" + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); } catch (Exception e) { log.Error(e.Message + "\n" + e.StackTrace + " at line:" + new StackTrace(true).GetFrame(0).GetFileLineNumber()); Console.WriteLine(e.Message + "\n" + e.StackTrace); Assert.AreEqual(true, false); } }
public WiKiInformation SearchInformation(string url) { if (url == null) { return(null); } WiKiInformation wiKiInformation = new WiKiInformation(); var doc = NSoup.NSoupClient.Parse(web.getHtml(url)); var class_text = NSoup.NSoupClient.Parse(doc.GetElementsByClass("class-text").ToString()); var class_title = NSoupClient.Parse(class_text.ToString()); wiKiInformation.Title = class_title.GetElementsByTag("h3").Text; NSoup.Select.Elements class_info = class_text.GetElementsByClass("class-info-left");; var link = NSoupClient.Parse(class_info.ToString()).GetElementsByClass("list"); List <ExternalLinkItem> list = new List <ExternalLinkItem>(); foreach (var item in NSoupClient.Parse(link.ToString()).GetElementsByTag("li")) { var item_link = NSoupClient.Parse(item.ToString()).GetElementsByTag("script").ToString(); string Link = item_link.Replace("{title:\"这是一个站外链接\",content:\"<p>此链接会跳转到:</p><p><strong>", "|").Replace("</strong>", "|").Split('|')[1]; string Title = NSoupClient.Parse(item.ToString()).GetElementsByTag("span").Attr("title").ToString(); list.Add(new ExternalLinkItem { url = Link, Title = Title }); } wiKiInformation.ExternalLinks = list.ToArray(); var mcver = NSoupClient.Parse(NSoupClient.Parse(class_info.ToString()).GetElementsByTag("li").ToString()).GetElementsByTag("ul"); List <string> version = new List <string>(); foreach (var i in mcver) { var mc = NSoupClient.Parse(i.GetElementsByClass("text-danger").ToString()).GetElementsByTag("a"); bool tf = false; foreach (var j in mc) { if (!string.IsNullOrEmpty(j.Text())) { version.Add(j.Text()); tf = true; } } if (tf) { break; } } wiKiInformation.SupportVersion = version.ToArray(); var text_area = NSoupClient.Parse(NSoupClient.Parse(NSoupClient.Parse(NSoupClient.Parse(doc.GetElementsByClass("center").ToString()).GetElementsByTag("ul").ToString()).GetElementsByTag("li").ToString()).GetElementsByClass("common-text").ToString()).GetElementsByTag("p"); string text = ""; List <string> Img = new List <string>(); foreach (var i in text_area) { text += i.Text() + "\n"; var imgUrl = i.GetElementsByTag("Img").Attr("src").ToString(); if (!string.IsNullOrEmpty(imgUrl)) { Img.Add("https://" + imgUrl); } } wiKiInformation.introduce = text; wiKiInformation.ImgUrl = Img.ToArray(); var class_relation_list = doc.GetElementsByClass("class-relation-list"); try { var relation = NSoupClient.Parse(NSoupClient.Parse(class_relation_list[0].ToString()).GetElementsByClass("relation")[0].ToString()).GetElementsByTag("li"); string test = relation.ToString(); if (test.IndexOf("前置") < 0) { wiKiInformation.PreMod = null; return(wiKiInformation); } List <PreModItem> relation_list = new List <PreModItem>(); for (var i = 1; i < relation.Count; i++) { var mod = relation[i].GetElementsByTag("a"); var modtitle = mod.Attr("data-original-title").ToString(); var wikiLink = "https://www.mcmod.cn/" + mod.Attr("href").ToString(); relation_list.Add(new PreModItem { ModName = modtitle, WikiUrl = wikiLink }); } wiKiInformation.PreMod = relation_list.ToArray(); } catch (Exception ex) { } return(wiKiInformation); }
public void Execute() { SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=GZMgzm123"); con.Open(); string sql = "select UserID,UserName from [User]"; SqlCommand sc = new SqlCommand(sql, con); SqlDataReader sr = sc.ExecuteReader(); Dictionary <string, int> unload = new Dictionary <string, int>(); Dictionary <string, int> loaded = new Dictionary <string, int>(); while (sr.Read()) { unload.Add(sr[1].ToString(), Convert.ToInt32(sr[0])); } sr.Close(); while (unload.Count > 0) { string name = unload.First().Key; string url = "https://social.msdn.microsoft.com/Profile/" + name + "/activity/"; int id = unload.First().Value; try { //Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!"); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Accept = "text/html"; req.UserAgent = "Mozilla/5.0 (Windows NT 10.0;Win64;x64)"; string html = null; HttpWebResponse res = (HttpWebResponse)req.GetResponse(); using (StreamReader reader = new StreamReader(res.GetResponseStream())) { html = reader.ReadToEnd(); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html); SqlCommand findc = new SqlCommand(); findc.CommandType = CommandType.Text; findc.Connection = con; DateTime newDateTime = DateTime.Now; SqlDataReader scc; NSoup.Select.Elements ele = doc.Select("div#Activities>div"); int i = 0; foreach (NSoup.Nodes.Element ee in ele) { string dt = ee.Select("div.activity-date").Attr("title"); string content = ee.Select("div.activity-detail").Text; string href = ee.Select("div.activity-detail>a").First().Attr("href"); //href = href.Substring(href.IndexOf("<a"), href.IndexOf("/a>")); dt = dt.Substring(dt.IndexOf("Date(") + 5); dt = dt.Substring(0, dt.IndexOf(")")); double unixDate = Convert.ToDouble(dt); DateTime start1 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime date = new DateTime(); date = start1.AddMilliseconds(unixDate).ToLocalTime(); TimeSpan ts = newDateTime - date; if (content.Contains("'")) { content = content.Replace("'", "''"); } //string insql = "select * from [Member] where reply_content='" + content + "' and reply_time='" + date + "' and reply_id=" + id; string insql = "select * from [Member] where reply_url='" + href + "'and reply_id=" + id; findc.CommandText = insql; scc = findc.ExecuteReader(); if (scc.Read()) { scc.Close(); i++; continue; /*if (i < 3) * { * continue; * } * else * { * i = 0; * break; * }*/ } else { scc.Close(); insql = "INSERT INTO [Member](reply_id,member_name,reply_content,reply_time,reply_url) VALUES('" + id + "','" + name + "',N'" + content + "','" + date + "','" + href + "')"; SqlCommand ff = new SqlCommand(insql, con); ff.ExecuteNonQuery(); } } unload.Remove(name); loaded.Add(name, id); } } catch (WebException we) { con.Close(); Console.WriteLine(we.Message); } } sr.Close(); con.Close(); }
private void download2() { // 下载地址 string url = textBox1.Text; string html = getHtml(url); NSoup.Nodes.Document d = NSoup.NSoupClient.Parse(html); //获取标题 String title = d.GetElementsByClass("tit").First.GetElementsByTag("h1").Text; //如果没有标题就用毫秒数 title = (title != null && !title.Equals(""))?title:DateTime.Now.ToUniversalTime().Ticks + ""; NSoup.Nodes.Element el = d.GetElementById("vlink_1"); NSoup.Select.Elements es = el.GetElementsByTag("li"); richTextBox1.Text = ""; foreach (var e in es) { string subHtml = getHtml("http://www.ting56.com" + e.GetElementsByTag("a").Attr("href")); Document d1 = NSoup.NSoupClient.Parse(subHtml); Match mc = Regex.Match(subHtml, "FonHen_JieMa\\('([0-9,*]*)'\\)"); //获取加密url string miwen = mc.Groups[1].Value; string[] tArr = Regex.Split(miwen, "\\*", RegexOptions.IgnoreCase); int n = tArr.Length; string s = ""; for (int i = 0; i < n; i++) { if (!tArr[i].Equals("")) { s += (char)int.Parse(tArr[i]); } } //下载地址 string downUrl = Regex.Split(s, "\\&", RegexOptions.IgnoreCase)[0]; //文件扩展名 string downFileExt = Regex.Split(s, "\\&", RegexOptions.IgnoreCase)[2]; string path = textBox2.Text + "\\" + title + "\\"; string fileName = e.Text(); string localFile = path + fileName + downFileExt; // MessageBox.Show(localFile); richTextBox1.AppendText("开始下载 " + fileName + "\n\r" + downUrl + "\n\r"); //用线程执行下载 // Thread oGetArgThread = new Thread(new ThreadStart(HttpDownload)); // oGetArgThread.IsBackground = true; // oGetArgThread.Start(); // DownFile hd = new DownFile(); // hd.downUrl=downUrl; // hd.localFile=localFile; // DownFile df = new DownFile(downUrl,localFile); // ThreadPool.QueueUserWorkItem(new WaitCallback(df.HttpDownload)); // Thread oGetArgThread = new Thread(new ThreadStart(hd.HttpDownload)); // oGetArgThread.IsBackground = true; // oGetArgThread.Start(); HttpDownload(downUrl, localFile); } // MessageBox.Show(title); }
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; string ikoktest = activity.Text; //byte[] utf8bytes = System.Text.Encoding.Default.GetBytes(ikoktest); //byte[] utf8bytes2 = System.Text.Encoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.UTF8, utf8bytes); //utf8bytes2.ToString(); //当前文有重复的基本意图的时候,取到最近的基本意图 if (intention.Split(',').Length > 1 && !intention.Equals("")) { intention = intention.Substring(intention.LastIndexOf(",") + 1); } //@@@intention_head MatchCollection mc_study_words = Regex.Matches(ikoktest, ".*教.*你(一些)*(部分)*(一点)*(?<100>.{1,2})词"); bool is_study = Regex.IsMatch(ikoktest, ".*教.*你(一些)*(部分)*(一点)*(?<100>.{1,2})词"); if (is_study) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_study; talking_depth = 0; } MatchCollection mc_action = Regex.Matches(ikoktest, "[.]*放(一首){0,1}(?<musician>.*)(的){0,1}歌[\\.。!]*"); bool is_action = Regex.IsMatch(ikoktest, "[.]*放(一首){0,1}(.*)(的){0,1}歌[\\.。!]*"); if (is_action) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_action; talking_depth = 0; } MatchCollection mc_check = Regex.Matches(ikoktest, "(.*查(一下){0,1}(?<check>.+)(是什么){0,1}|(?<check>.+)是(什么|啥))|(?<check>^怎么{0,1}样{0,1}.+)"); bool is_check = Regex.IsMatch(ikoktest, "(.*查(一下){0,1}(?<check>.+)(是什么){0,1}|(?<check>.+)是(什么|啥))|(?<check>^怎么{0,1}样{0,1}.+)"); if (is_check) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_check; talking_depth = 0; } MatchCollection mc_train = Regex.Matches(ikoktest, ".*(训练模式|训练你)"); bool is_train = Regex.IsMatch(ikoktest, ".*(训练模式|训练你)"); if (is_train) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_train; talking_depth = 0; } if (intention.Equals("train") || intention == "train") { foreach (Match item in mc_train) { await context.PostAsync("请问您是要开启训练模式吗?"); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm) { MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); MySqlCommand mycmd_s = new MySqlCommand("select value from Ssubject", conn); MySqlCommand mycmd_p = new MySqlCommand("select value from predicate", conn); MySqlCommand mycmd_a = new MySqlCommand("select value from accusative", conn); string result_s = ""; string result_p = ""; string result_a = ""; MySqlDataReader reader = null; reader = mycmd_s.ExecuteReader(); while (reader.Read()) { allSsubjects.Add(reader[0].ToString()); result_s += reader[0].ToString() + ","; } reader.Close(); reader = mycmd_p.ExecuteReader(); while (reader.Read()) { allPredicate.Add(reader[0].ToString()); result_p += reader[0].ToString() + ","; } reader.Close(); reader = mycmd_a.ExecuteReader(); while (reader.Read()) { allAccusative.Add(reader[0].ToString()); result_a += reader[0].ToString() + ","; } result_s = result_s.Remove(result_s.LastIndexOf(","), 1); result_p = result_p.Remove(result_s.LastIndexOf(","), 1); result_a = result_a.Remove(result_s.LastIndexOf(","), 1); talking_depth++; await context.PostAsync("好的,我已经学会的主语有: " + result_s + "。谓语有:" + result_p + "。宾语有:" + result_a + "。您可以在此基础上训练我,也可以继续让我学习哦~~"); reader.Close(); conn.Close(); } } if (temp == 2) { foreach (string every in allPredicate) { await context.PostAsync(every); MatchCollection mc_train_words_confirm = Regex.Matches(ikoktest, "(?<Ssubjective>.{0,3})" + every + "(?<accusative>.{1,3})[\\.;。;]*"); bool confirm = Regex.IsMatch(ikoktest, "(?<Ssubjective>.{0,3})" + every + "(?<accusative>.{1,3})[\\.;。;]*"); if (confirm) { current_Predicate = every; foreach (Match item in mc_train_words_confirm) { current_Ssubject = item.Groups["Ssubjective"].Value; current_Accusative = item.Groups["accusative"].Value; } //改变基本元素的权值 MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); MySqlCommand mycmd_1 = new MySqlCommand("update Ssubject set authority = authority+1 where value = '" + current_Ssubject + "';", conn); MySqlCommand mycmd_2 = new MySqlCommand("update predicate set authority= authority +1 where value = '" + current_Predicate + "';", conn); MySqlCommand mycmd_3 = new MySqlCommand("update accusative set authority= authority +1 where value = '" + current_Accusative + "';", conn); if (mycmd_1.ExecuteNonQuery() > 0) { MySqlDataReader mr = new MySqlCommand("select id from Ssubject where value = '" + current_Ssubject + "';", conn).ExecuteReader(); while (mr.Read()) { current_Ssubject_id = Int16.Parse(mr[0].ToString()); } mr.Close(); } if (mycmd_3.ExecuteNonQuery() > 0) { MySqlDataReader mr = new MySqlCommand("select id from accusative where value = '" + current_Accusative + "';", conn).ExecuteReader(); while (mr.Read()) { current_Accusative_id = Int16.Parse(mr[0].ToString()); } mr.Close(); } if (mycmd_2.ExecuteNonQuery() > 0) { MySqlDataReader mr = new MySqlCommand("select id from predicate where value = '" + current_Predicate + "';", conn).ExecuteReader(); while (mr.Read()) { current_Predicate_id = Int16.Parse(mr[0].ToString()); } mr.Close(); } //改变对应关系的权值 MySqlConnection conn1 = new Init_DB().get_Init_DB(); conn1.Open(); if (current_Ssubject_id != 0) { MySqlCommand currentcmd_1 = new MySqlCommand("select * from s_p where s_id = " + current_Ssubject_id + " and p_id = " + current_Predicate_id, conn1); if (currentcmd_1.ExecuteNonQuery() > 0) { MySqlCommand thecmd = new MySqlCommand("update s_p set authority = authority+1 where s_id =" + current_Ssubject_id + " and p_id =" + current_Predicate_id, conn1); thecmd.ExecuteNonQuery(); } else { MySqlCommand thecmd = new MySqlCommand("insert into s_p(s_id,p_id) values(" + current_Ssubject_id + "," + current_Predicate_id + ")", conn1); thecmd.ExecuteNonQuery(); } } if (current_Accusative_id != 0) { MySqlCommand currentcmd_2 = new MySqlCommand("select p_id,a_id from p_a where p_id =" + current_Predicate_id + " and a_id =" + current_Accusative_id, conn1); if (currentcmd_2.ExecuteNonQuery() > 0) { MySqlCommand thecmd = new MySqlCommand("update p_a set authority = authority+1 where p_id =" + current_Predicate_id + " and a_id = " + current_Accusative_id, conn1); thecmd.ExecuteNonQuery(); } else { MySqlCommand thecmd = new MySqlCommand("insert into p_a(p_id,a_id) values(" + current_Predicate_id + "," + current_Accusative_id + ")", conn1); thecmd.ExecuteNonQuery(); } } conn1.Close(); await context.PostAsync("本次训练完成!"); break; } } } } if (intention.Equals("check") || intention == "check") { foreach (Match item in mc_check) { await context.PostAsync("请问您是要查" + item.Groups["check"].ToString() + "吗?"); thecache = item.Groups["check"].ToString(); await context.PostAsync(thecache); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm_check = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm_check) { await context.PostAsync("正在为您执行命令...."); talking_depth++; if (thecache != null && !thecache.Equals("")) { await context.PostAsync("正在为您查询...."); //thecache=System.Web.HttpUtility.UrlEncode(thecache, Encoding.UTF8); if (Regex.IsMatch(thecache, "^怎么{0,1}样{0,1}")) { org.jsoup.nodes.Document docsource = org.jsoup.Jsoup.connect("https://jingyan.baidu.com/search?word=" + thecache).get(); // await context.PostAsync(HtmlString); //NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect("https://jingyan.baidu.com/search?word=" + thecache).Get(); //await context.PostAsync(doc.ToString()); //NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(HtmlString); //await context.PostAsync("runrurun2"+doc.Html()); // WebClient webClient = new WebClient(); //webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); // String HtmlString = Encoding.GetEncoding("utf-8").GetString(webClient.DownloadData("https://jingyan.baidu.com/search?word=" + thecache)); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(docsource.body().toString()); NSoup.Select.Elements e = doc.Select(".search-list").Select("dl").First.Select("dt").Select("a"); await context.PostAsync(e.ToString()); string url = e.Attr("href"); String aim_Html = org.jsoup.Jsoup.connect("https://jingyan.baidu.com" + url).get().toString(); NSoup.Nodes.Document doc_1 = NSoup.NSoupClient.Parse(aim_Html); string aimcontent = doc_1.Select("div.exp-content-block").Select("li[class^=exp-content-list]").Text; aimcontent.Replace("步骤阅读", ""); aimcontent.Replace("END", ""); Regex.Replace(aimcontent, "\\s[0-9]{0,2}\\s", "\\n\\s[0-9]{0,2}\\s"); await context.PostAsync(aimcontent); } else { thecache = thecache.Replace("是什么", ""); String HtmlString = Encoding.GetEncoding("utf-8").GetString(new WebClient().DownloadData("https://baike.baidu.com/item/" + thecache)); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(HtmlString); await context.PostAsync(doc.Select(".lemma-summary").First.Text()); } //NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect("https://baike.baidu.com/item/JJ").Get(); //Console.Write(doc.Text()); await context.PostAsync("查询成功,欢迎下次光临"); } } } } if (intention.Equals("action") || intention == "action") { foreach (Match item in mc_action) { await context.PostAsync("您要放" + item.Groups["musician"].ToString() + "歌吗?"); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "[对是好嗯]+的*"); if (confirm) { await context.PostAsync("正在为您执行命令...."); Process myProcess = new Process(); // try{ myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.FileName = "C:\\Program Files (x86)\\kuwo\\kuwomusic\\8.5.2.0_UG6\\bin\\KwMusic.exe"; myProcess.StartInfo.CreateNoWindow = true; myProcess.Start(); //} //catch (Exception e){ //} } } } if (intention.Equals("study") || intention == "study") { foreach (Match item in mc_study_words) { await context.PostAsync("您要教我" + item.Groups[100].ToString() + "词吗?"); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm) { talking_depth++; await context.PostAsync("请问它们将要在句子中作什么成分?我暂时只能学习主语,谓语和宾语哟~~"); } } if (temp == 2) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^(?<grammer>[主谓宾])语{0,1}"); bool confirm = Regex.IsMatch(ikoktest, "^(?<grammer>[主谓宾])语{0,1}"); if (confirm) { foreach (Match item in mc_study_words_confirm) { thecache = item.Groups["grammer"].Value; } talking_depth++; await context.PostAsync("好的,我知道了。。您请说吧!"); } } if (temp == 3) { MatchCollection mc_study_words_content = Regex.Matches(ikoktest, "([^\\d]{1,2})[,,\\.。;;]*"); string aim_table = ""; if (thecache.Equals("主")) { aim_table = "Ssubject"; } if (thecache.Equals("谓")) { aim_table = "predicate"; } if (thecache.Equals("宾")) { aim_table = "accusative"; } if (aim_table != null && !aim_table.Equals("")) { foreach (Match item in mc_study_words_content) { await context.PostAsync("您说的词语是:" + item.Groups[1].Value + ",正在学习...."); MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); Regex reg = new Regex("[,,\\.。;;]"); string content = reg.Replace(item.Groups[1].Value, ""); MySqlCommand mycmd = new MySqlCommand("insert into " + aim_table + "(value) values('" + content + "')", conn); if (mycmd.ExecuteNonQuery() > 0) { await context.PostAsync("已成功学习!"); } conn.Close(); } } intention = ""; } } //@@@intention_body if (intention == "" || intention.Equals("")) { await context.PostAsync("不知道您要干什么呢~~需要学习吗?"); int temp = talking_depth + 1; if (temp == 1) { current_intent_id = new Train().train_intentid(ikoktest, allSsubjects, allPredicate, allAccusative); MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm) { MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); MySqlDataReader mr = new MySqlCommand("select id from intent where value = '" + thecache + "';", conn).ExecuteReader(); while (mr.Read()) { Int16.Parse(mr[0].ToString()); } mr.Close(); intention = "studying"; await context.PostAsync("好的,请遵照提示配置您的对话模板。"); await context.PostAsync("请输入您的命令类型:(a)对话型命令或者(b)功能型!默认为对话型"); } } } if (intention == "studying" || intention.Equals("studying")) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "b"); bool confirm = Regex.IsMatch(ikoktest, "b"); int temp = talking_depth + 1; if (temp == 1) { if (confirm) { talking_unit.type = 1; talking_depth++; await context.PostAsync("请输入您要完成的功能:(目前已经实现的功能有:打开,关闭,查询,订购。更多功能正在开发中!请根据提示回复必要的信息已实现具体的功能!)"); } else { talking_depth++; await context.PostAsync("请输入您的问题"); } } if (temp == 2) { MatchCollection mc_function_confirm = Regex.Matches(ikoktest, "^(?<function>打开|关闭|查询|订购)(?=\\s)"); bool confirm1 = Regex.IsMatch(ikoktest, "^(?<function>打开|关闭|查询|订购)(?=\\s)"); if (confirm1) { foreach (Match item in mc_function_confirm) { thecache = item.Groups["function"].Value; } talking_unit.order = thecache; talking_units.Add(talking_unit); talking_unit.initTalking_unit(); await context.PostAsync("是否需要进一步的操作?"); talking_depth = 3; } else { string question = new GetIntent().getMain(ikoktest, allSsubjects, allPredicate, allAccusative); talking_unit.question = question; talking_depth++; await context.PostAsync("请输入该问题的回答"); } } if (temp == 3) { string answer = ikoktest; talking_unit.answer = answer; talking_depth++; await context.PostAsync("是否需要进一步的操作?"); } if (temp == 4) { MatchCollection mc_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm2 = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm2) { talking_units.Add(talking_unit); talking_unit.initTalking_unit(); await context.PostAsync("好的,请遵照提示配置您的对话模板。"); await context.PostAsync("请输入您的命令类型:(a)对话型命令或者(b)功能型!默认为对话型"); talking_depth = 0; } else { MySqlConnection conn1 = new Init_DB().get_Init_DB(); conn1.Open(); //将记录存到数据库中 foreach (Talking_unit item in talking_units) { int i = 0; new MySqlCommand("insert into talking_unit(depth,intended_id,type,question,answer,order) values(" + i + "," + current_intent_id + "," + item.type + "," + item.question + "," + item.answer + "," + item.order + ")", conn1); i++; } conn1.Close(); } } } await context.PostAsync("本次的基本意图是:" + intention); await context.PostAsync("本次的谈话深度是:" + talking_depth); // if (activity.Text.Contains("你好")) // { // await context.PostAsync("你好,老铁"); // } // else if (activity.Text.Contains("你叫什么名字")) // { // await context.PostAsync("你就叫我特浪铺吧。"); // } // else if (activity.Text.Contains("你有对象吗")) // { // await context.PostAsync("不要问这么悲伤的问题啊! 扎心了 老铁。。。"); // } // else { // await context.PostAsync("你在用脸滚键盘么。。。你发的什么我看不懂"); // } // int length = (activity.Text ?? string.Empty).Length; // await context.PostAsync($"you sent {activity.Text}which was{length} characters"); // context.Wait(MessageReceivedAsync); }