|
![](https://clubstatic.excelhome.net/image/common/ico_lz.png)
楼主 |
发表于 2016-1-14 21:32
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
VSTO测试成功,主要代码如下:
- using HtmlAgilityPack;
- 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;
- }
- private void button5_Click(object sender, EventArgs e)
- {
- comboBox1.Items.Clear();
- //更加链接格式和省份代码构造URL
- String url = "http://www.tianqihoubao.com/lishi/";
- //下载网页源代码
- 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[7]/div[1]/div[1]/div[3]");
- if (res != null)
- {
- var list = res.SelectNodes(@"dl");//选择标签数组
- if (list.Count < 1) return;
- foreach (var item in list)
- {
- var dd = item.SelectSingleNode(@"dt").SelectNodes("a");
- foreach (var node in dd)
- {
- var text = node.InnerText.Trim();
- //拼音代码要从href属性中进行分割提取
- var herf = node.Attributes["href"].Value.Trim().Split('/', '.');
- //Console.WriteLine("{0}:{1}", text, herf[herf.Length - 2]);
- comboBox1.Items.Add(text + ":" + herf[herf.Length - 2]);
- }
- }
- }
- comboBox1.TabIndex = 0;
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- comboBox2.Items.Clear();
- try
- {
- String cityCode = comboBox1.Text.Substring(comboBox1.Text.LastIndexOf(":") + 1);
- //更加链接格式和省份代码构造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) return;
- foreach (var item in list)
- {
- var dd = item.SelectSingleNode(@"dd").SelectNodes("a");
- foreach (var node in dd)
- {
- var text = node.InnerText.Trim();
- //拼音代码要从href属性中进行分割提取
- var herf = node.Attributes["href"].Value.Trim().Split('/', '.');
- //Console.WriteLine("{0}:{1}", text, herf[herf.Length - 2]);
- comboBox2.Items.Add(text + ":" + herf[herf.Length - 2]);
- }
- }
- }
- }
- catch (WebException webEx)
- {
- Console.WriteLine(webEx.Message.ToString());
- }
- }
- private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- try
- {
- String cityCode = comboBox2.Text.Substring(comboBox2.Text.LastIndexOf(":") + 1);
- //更加拼音代码,月份信息构造URL
- String url = String.Format("http://www.tianqihoubao.com/lishi/{0}/month/{1}{2}.html", cityCode, comboBox3.Text, comboBox4.Text);
- //获取该链接的源代码
- 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)
- {
- 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();
- //输出
- //Console.WriteLine("{0}:{1},{2},{3}", date1, tq, qw, fx);
- listBox1.Items.Add(date1 + ":" + tq + ":" + qw + ":" + fx);
- }
- }
- }
- catch (WebException webEx)
- {
- Console.WriteLine(webEx.Message.ToString());
- }
- }
复制代码 |
|