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.Net;
using System.IO;
using System.Text.RegularExpressions;

using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Filters;

namespace Leo.GetOrganzation
{
    public partial class FormLeo : Form
    {
        IList<string> siteList = new List<string>();

public FormLeo()
        {
            InitializeComponent();
            this.InitSet();
        }

private void InitSet()
        {
            this.comboBoxArea.SelectedIndex = 0;
        }

private void comboBoxArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strUrl = string.Empty;
            switch (this.comboBoxArea.SelectedIndex)
            {
                case 0:
                    strUrl = string.Empty;
                    break;
                case 1:
                    strUrl = "http://www.alexa.com/topsites/global;{0}";
                    break;
                case 2:
                    strUrl = "http://www.alexa.com/topsites/countries;{0}/CN";
                    break;
                case 3:
                    strUrl = "http://www.alexa.com/topsites/countries;{0}/HK";
                    break;
                case 4:
                    strUrl = "http://www.alexa.com/topsites/countries;{0}/MO";
                    break;
                case 5:
                    strUrl = "http://www.alexa.com/topsites/countries;{0}/TW";
                    break;
            }

this.txtUrlText.Text = strUrl;

this.textBox1.Text = "";
            this.textBox2.Text = "";
            this.textBox3.Text = "";

}

private void btnGetRes_Click(object sender, EventArgs e)
        {
            siteList = new List<string>();
            if (this.comboBoxArea.SelectedIndex == 0)
            {
                MessageBox.Show("请选择区域");
                return;
            }

for (int i = 0; i < int.Parse(this.textBox3.Text.Trim()); i++)
            {

string urlLink = string.Format(this.txtUrlText.Text.Trim(),i);
                string temp = this.GetRes(urlLink);

this.GetSiteName(temp);

System.Threading.Thread.Sleep(1000);
                Application.DoEvents();
            }

////string temp = this.GetRes("http://www.alexa.com/topsites/countries;19/TW");

////this.GetSiteName(temp);

MessageBox.Show("获取完毕,待导出");
        }

private void GetSiteName(string strhtml)
        {
            Parser parser = Parser.CreateParser(strhtml, null);
            NodeList nodes = parser.ExtractAllNodesThatMatch(new TagNameFilter("h2"));
          
            for (int i = 0; i < nodes.Count; i++)
            {
                siteList.Add(nodes[i].Children[1].ToPlainTextString());
            }
           
        }

private string GetRes(string Url)
        {

string strResult = string.Empty;
            try
            {
                HttpWebRequest request =(HttpWebRequest)WebRequest.Create(Url);
                request.Timeout = 30000;
                request.Headers.Set("Pragma", "no-cache");

HttpWebResponse response =(HttpWebResponse)request.GetResponse();

Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.GetEncoding("utf-8");
                StreamReader streamReader =new StreamReader(streamReceive, encoding);
                strResult = streamReader.ReadToEnd();
            }
            catch(Exception) {}

return strResult;
        }

private void WriteTxt()
        {
            string sucessFile = Application.StartupPath;
            string selectText = this.comboBoxArea.SelectedItem.ToString();
            if (File.Exists(string.Format("{0}\\{1}.txt", sucessFile, selectText)))
            {
                File.Delete(string.Format("{0}\\{1}.txt", sucessFile, selectText));
            }
            StreamWriter sw = new StreamWriter(string.Format("{0}\\{1}.txt", sucessFile, selectText),true);
            try
            {
                foreach (string item in siteList)
                {
                    sw.WriteLine(item+" "+selectText);
                }
                sw.Dispose();
                sw.Close();
                MessageBox.Show("导出OK");
               
            }
            catch (Exception)
            {
            }
        }

private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBoxArea.SelectedIndex == 0)
            {
                MessageBox.Show("请选择区域");
                return;
            }
            if (siteList.Count == 0)
            {
                MessageBox.Show("没有数据");
                return;
            }
            WriteTxt();
        }

private void textBox3_TextChanged(object sender, EventArgs e)
        {
           
            int countPage = int.Parse(this.textBox3.Text.Trim());
            this.textBox1.Text = string.Format(this.txtUrlText.Text.Trim(), 0);
            this.textBox2.Text = string.Format(this.txtUrlText.Text.Trim(), countPage - 1);
        }

}
}

Winista.Text.HtmlParser; 获取html的更多相关文章

  1. HTMLParser获取属性名

    HTMLParser获取属性名方式: 原始网页文本: <a title="美军被曝虐尸" href="http://www.sogou.com/web?query= ...

  2. jquery中html()或text()方法获取或设置p标签的值

    html()方法可以用来读取或者设置某个元素中的HTML内容,text()方法可以用来读取或者没置某个元素中的文本内容 html()方法 此方法类似于JavaScript中的innerHTML属性,可 ...

  3. select获取选中的option(包含value和text,重点是text怎么获取)

    简单描述:后台需要获取到select标签选择的内容,也就是text,该怎么取呢?很简单. 代码: //hml代码<div class="col-md-6"> <l ...

  4. js 通过html()及text()方法获取并设置p标签的显示值

    html()方法 此方法类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的HTML内容.要获取某个元素的内容,可以这样: 复制代码 代码如下: var p_html ...

  5. 大数据处理之道 (htmlparser获取数据&lt;一&gt;)

    一:简单介绍 (1)HTML Parser是一个用于解析Html的Java的库.可採用线性或嵌套两种方式.主要用于网页的转换或提取,他有一些特性:过滤器filter,遍历器visitors,通常的标签 ...

  6. Text 尺寸获取

    获取text在当前文本内容下应该尺寸: 宽度:text.preferredWidth 高度:text.preferredHeight

  7. XPath匹配标签使用text()判断获取结果失败/为空的问题及解决方法

    XPath当匹配标签判断text()判断内容失败的问题及解决 问题复现 在爬取网站的时候我使用XPath去抓取网页上的内容,XPath表达式来精准获取需要的标签内容. 当我对如下一段html代码编写X ...

  8. sublime text 插件 -- 获取文件名到剪贴板

    日常开发使用 sublime text 有好长一段时间了,有时候想拷贝当前正在编辑的文件名时发现没有很快捷的方法,一般都是先点击右键菜单栏中的 Reveal in Side Bar 对文件进行定位(在 ...

  9. HTMLPARSER.NET 参考资料

    例子1: using System;using System.IO;using Winista.Text.HtmlParser;using Winista.Text.HtmlParser.Lex;us ...

随机推荐

  1. Storm0.9.4安装 - OPEN 开发经验库

    Storm0.9.4安装 - OPEN 开发经验库 bin/zkServer.sh start /home/guym/down/kafka_2.8.0-0.8.0/config/zookeeper.p ...

  2. ASP.NET MVC 页面调整并传递参数

    转自:http://blog.csdn.net/zhensoft163/article/details/7174661 使用过ASP.NET MVC的人都知道在MVC中页面后台中常用的页面跳转方法有几 ...

  3. mysqlbinlog快速遍历搜索记录 (转)

    目标,开发人员说有个数据莫名其妙添加了,但是不知道是从哪里添加的,而且应用功能里面不应该添加这样的数据,为了查清楚来源,所 以我就准备去binlog里面找了,但是binlog有好几个月的数,我这样一个 ...

  4. MySql之on duplicate key update详解

    在我们的日常开发中,你是否遇到过这种情景:查看某条记录是否存在,不存在的话创建一条新记录,存在的话更新某些字段.你的处理方式是不是就是按照下面这样? $result = mysql_query('se ...

  5. R(五): R常用函数

    工作笔记记录,会持续更新.... 目录: apply tapply lapply sapply merge substr.substring.strsplit.unlist.paste.paste0. ...

  6. javascript 对象中的 handleEvent

    在高级浏览器中,我们在绑定事件的时候 可以知道绑定一个对象,然后在这个对象中的 handleEvent 方法会自动进入指定的方法,不多说了举个例子吧!! var events = { handleEv ...

  7. 每日学习心得:Linq解决DataTable按照某一列的值排序问题/DataTable 导出CSV文件/巧用text-overflow解决数据绑定列数据展示过长问题

    2013-8-5 1 Linq解决DataTable按照某一列的值排序 在之前的总结中提到过对拼接而成的复合的DataTable按照某一列值的大小排序,那个主要的思想是在新建表结构时将要排序的那一列的 ...

  8. Linux下访问其他机器的共享

    1.如何查看其他机器上的共享列表? 解答:使用smbclient 客户端,在Linux机器上可以用来查看服务器上的共享资源,也可以向ftp一样,用户可以登陆samba服务器,也可以上传put和下载ge ...

  9. 【jmeter】JMeter处理Cookie与Session

    有些网站保存信息是使用Cookie,有些则是使用Session.对于这两种方式,JMeter都给予一定的支持. 1.Cookie 添加方式:线程组-配置元件-HTTP Cookie 管理器,如下图: ...

  10. matlab 画三维图函数

    matlab三维绘图 http://blog.sina.com.cn/s/blog_6d5ffd0d0100lyah.html Matlab绘图系列之高级绘图 http://blog.163.com/ ...