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); }
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); }
public void RequestSubPage(ProgressHandler handler) { var lastColumn = this.DataSource.Columns.Count - 1; if (this.DataSource != null && this.DataSource.Rows.Count > 0) { int i = 1; if (!this.Login()) { MsgBox.Alert("登陆失败"); return; } foreach (DataRow row in DataSource.Rows) { handler(row, new ProgressEventArgs() { Value = i }); i++; //防止网络慢,登录过期 if (i % 500 == 0) { this.Login(); } var urlAddress = BasePath + row[1]; Result result = base.Request(urlAddress); if (result.IsSuccess && !string.IsNullOrWhiteSpace(result.Msg)) { try { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(result.Msg); var links = doc.GetElementsByTag("a"); foreach (var item in links) { var text = item.Text(); if (text.Contains("下载视频") && text.Contains("鼠标右键另存为")) { var downloadUrl = Utils.GetAttr(item, "href"); row[2] = downloadUrl; row[3] = Path.GetFileName(downloadUrl); break; } } } catch (Exception ex) { row[lastColumn] = ex.Message; } } } } }
/// <summary> /// 根据HTML内容,获取所有img标签 /// </summary> /// <param name="content">HTML内容</param> /// <returns></returns> public static List <string> GetImageTagList(string content) { List <string> imgList = null; //解析 try { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(content); imgList = doc.GetElementsByTag("img").Select(a => a.ToString()).ToList(); } catch { imgList = new List <string>(); } return(imgList); }
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()); } }
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> /// 根据HTML内容,获取所有img标签Url /// </summary> /// <param name="content">HTML内容</param> /// <param name="isUri">是否补充URL</param> /// <returns></returns> public static List <string> GetImageUrlList(string content, bool isUri = true) { List <string> imgList = null; //解析 try { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(content); imgList = doc.GetElementsByTag("img").Select(a => a.Attributes["src"]).ToList(); if (isUri) { imgList = imgList.Select(a => a.ToImageUrl()).ToList(); } } catch { imgList = new List <string>(); } return(imgList); }