https://github.com/exaphaser/ScrapySharp https://github.com/zzzprojects/html-agility-pack https://github.com/atifaziz/Fizzler https://archive.codeplex ...
https://github.com/exaphaser/ScrapySharp
https://github.com/zzzprojects/html-agility-pack
https://github.com/atifaziz/Fizzler
https://archive.codeplex.com/?p=fizzlerex
https://github.com/aspnet/blazor
https://github.com/SteveSanderson/Blazor
https://www.mathjax.org/#samples 數學公式
https://github.com/robinvanderknaap/MvcJqGrid
http://www.defenseinnovationmarketplace.mil/strategy.html
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Net; using System.Collections; using ScrapySharp; using ScrapySharp.Network; using ScrapySharp.Core; using HtmlAgilityPack; namespace HtmlAgilityPackDemo { /// <summary> /// HTML解析利器HtmlAgilityPack /// geovindu /// 塗聚文 /// 20180305 /// </summary> public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { this.textBox1.Text = "ln"; //List<CityList> lis=new List<CityList>(); } /// <summary> /// /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetWebClient(string url) { string strHTML = ""; WebClient myWebClient = new WebClient(); Stream myStream = myWebClient.OpenRead(url); StreamReader sr = new StreamReader(myStream, Encoding.Default);//註意編碼 strHTML = sr.ReadToEnd(); myStream.Close(); return strHTML; } /// <summary> /// nl /// </summary> /// <param name="cityCode"></param> public string ParsePageByArea(String cityCode, out List<CityList> listcity) { StringBuilder stp = new StringBuilder(); CityList city = null; List<CityList> clits = new List<CityList>(); //更加鏈接格式和省份代碼構造URL String url = String.Format("http://www.tianqihoubao.com/lishi/{0}.htm", cityCode); //下載網頁源代碼 var docText = GetWebClient(url); //載入源代碼,獲取文檔對象 var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(docText); //更加xpath獲取總的對象,如果不為空,就繼續選擇dl標簽 var res = doc.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/div[1]/div[6]/div[1]/div[1]/div[3]"); if (res != null) { var list = res.SelectNodes(@"dl");//選擇標簽數組 if (list.Count < 1) { listcity = clits; return ""; } foreach (var item in list) { var dd = item.SelectSingleNode(@"dd").SelectNodes("a"); foreach (var node in dd) { city = new CityList(); var text = node.InnerText.Trim(); //拼音代碼要從href屬性中進行分割提取 var herf = node.Attributes["href"].Value.Trim().Split('/', '.'); string str= string.Format("{0}:{1}", text, herf[herf.Length - 2]); city.CityName = text; city.CityCode = herf[herf.Length - 2]; stp.Append("\r\n" + str); clits.Add(city); } } } listcity = clits; return stp.ToString(); } /// <summary> /// http://www.tianqihoubao.com/lishi/dalian/month/201802.html /// </summary> /// <param name="cityCode"></param> /// <param name="year"></param> /// <param name="month"></param> public string ParsePageByCityMonth(String cityCode, Int32 year, Int32 month,out List<WeatherList> wea) { StringBuilder stp = new StringBuilder(); List<WeatherList> wlist = new List<WeatherList>(); WeatherList wt = null; //更加拼音代碼,月份信息構造URL String url = String.Format("http://www.tianqihoubao.com/lishi/{0}/month/{1}{2:D2}.html", cityCode, year, month); //獲取該鏈接的源代碼 var docText = GetWebClient(url); //載入源代碼,獲取頁面結構對象 var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(docText); //更加Xpath獲取表格對象 var res = doc.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/div[2]/div[6]/div[1]/div[1]/table[1]"); if (res != null) { //獲取所有行 var list = res.SelectNodes(@"tr"); list.RemoveAt(0);//移除第一行,是表頭 // 遍歷每一行,獲取日期,以及天氣狀況等信息 foreach (var item in list) { wt = new WeatherList(); var dd = item.SelectNodes(@"td"); //日期 - - 氣溫 - 風力風向 if (dd.Count != 4) continue; //獲取當前行日期 var date1 = dd[0].InnerText.Replace("\r\n", "").Replace(" ", "").Trim(); //獲取當前行天氣狀況 var tq = dd[1].InnerText.Replace("\r\n", "").Replace(" ", "").Trim(); //獲取當前行氣溫 var qw = dd[2].InnerText.Replace("\r\n", "").Replace(" ", "").Trim(); //獲取當前行風力風向 var fx = dd[3].InnerText.Replace("\r\n", "").Replace(" ", "").Trim(); //輸出 string str=string.Format("{0}:{1},{2},{3}", date1, tq, qw, fx); stp.Append(str); wt.Climate = tq; wt.Date =DateTime.Parse(date1); wt.Temperature = qw; wt.WindDirection = fx; wlist.Add(wt); } } wea = wlist; return stp.ToString(); } /// <summary> /// http://www.dusystem.com/geovindu.html /// ScrapingBrowser /// 獲取文件標題 /// </summary> /// <param name="url"></param> /// <returns></returns> public string getHtmlTitle(string url) { StringBuilder titl = new StringBuilder(); var uri = new Uri(url); var browser1 = new ScrapingBrowser(); var html1 = browser1.DownloadString(uri); var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html1); var html = doc.DocumentNode; var title = html.SelectNodes("title"); foreach (var htmlNode in title) { titl.Append(htmlNode.InnerText); } //CssSelect CssSelectAncestors var ps = html.SelectNodes("p").Elements("div#endText"); foreach (var htmlNode in ps) { titl.Append(htmlNode.InnerHtml); } return titl.ToString(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { List<CityList> list = new List<CityList>(); this.richTextBox1.Text = ParsePageByArea(this.textBox1.Text.Trim(),out list); this.comboBox1.DataSource = list; this.comboBox1.DisplayMember = "CityName"; this.comboBox1.ValueMember = "CityCode"; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { List<WeatherList> list = new List<WeatherList>(); int year=DateTime.Now.Year; int mont=DateTime.Now.Month-1; this.richTextBox2.Text = ParsePageByCityMonth(this.comboBox1.SelectedValue.ToString(), year, mont, out list); this.dataGridView1.DataSource = list; } } /// <summary> /// /// </summary> public class CityList { /// <summary> /// /// </summary> public string CityName { get; set; } /// <summary> /// /// </summary> public string CityCode { get; set; } } /// <summary> /// Climate, temperature, wind direction /// </summary> public class WeatherList { /// <summary> /// 氣候 /// </summary> public string Climate { get; set; } /// <summary> /// 溫度 /// </summary> public string Temperature { get; set; } /// <summary> /// 風向 /// </summary> public string WindDirection { get; set; } /// <summary> /// /// </summary> public DateTime Date { get; set; } } }