Winista.Text.HtmlParser; 获取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.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的更多相关文章
- HTMLParser获取属性名
HTMLParser获取属性名方式: 原始网页文本: <a title="美军被曝虐尸" href="http://www.sogou.com/web?query= ...
- jquery中html()或text()方法获取或设置p标签的值
html()方法可以用来读取或者设置某个元素中的HTML内容,text()方法可以用来读取或者没置某个元素中的文本内容 html()方法 此方法类似于JavaScript中的innerHTML属性,可 ...
- select获取选中的option(包含value和text,重点是text怎么获取)
简单描述:后台需要获取到select标签选择的内容,也就是text,该怎么取呢?很简单. 代码: //hml代码<div class="col-md-6"> <l ...
- js 通过html()及text()方法获取并设置p标签的显示值
html()方法 此方法类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的HTML内容.要获取某个元素的内容,可以这样: 复制代码 代码如下: var p_html ...
- 大数据处理之道 (htmlparser获取数据<一>)
一:简单介绍 (1)HTML Parser是一个用于解析Html的Java的库.可採用线性或嵌套两种方式.主要用于网页的转换或提取,他有一些特性:过滤器filter,遍历器visitors,通常的标签 ...
- Text 尺寸获取
获取text在当前文本内容下应该尺寸: 宽度:text.preferredWidth 高度:text.preferredHeight
- XPath匹配标签使用text()判断获取结果失败/为空的问题及解决方法
XPath当匹配标签判断text()判断内容失败的问题及解决 问题复现 在爬取网站的时候我使用XPath去抓取网页上的内容,XPath表达式来精准获取需要的标签内容. 当我对如下一段html代码编写X ...
- sublime text 插件 -- 获取文件名到剪贴板
日常开发使用 sublime text 有好长一段时间了,有时候想拷贝当前正在编辑的文件名时发现没有很快捷的方法,一般都是先点击右键菜单栏中的 Reveal in Side Bar 对文件进行定位(在 ...
- HTMLPARSER.NET 参考资料
例子1: using System;using System.IO;using Winista.Text.HtmlParser;using Winista.Text.HtmlParser.Lex;us ...
随机推荐
- Oracle报错,ORA-28001: 口令已经失效
Oracle11G创建用户时缺省密码过期限制是180天(即6个月), 如果超过180天用户密码未做修改则该用户无法登录. Oracle公司是为了数据库的安全性默认在11G中引入了这个默认功能,但是这个 ...
- hadoop-2.7.0
65 cd /home/guyumei/下载/ 66 ll 67 cd .. 68 ll 69 cd .. 70 ll 71 cd guyumei/ 72 ll 73 cd hadoop-2.7.0/ ...
- 搜索关注点--2014年的google关注点
补充: 网站知名网站收录 网站被世界三大知名网站DMOZ,Yahoo和Looksmart收录 众所周知,Google的Pagerank系统对那些门户网络目录如 DMOZ,Yahoo和Looksmart ...
- eclipse安装完成后配置tomcat server runtime evironment
安装eclipse后创建web项目需要运行在tomcat服务器上,这里是tomcat的配置:(前提是电脑上已经安装了tomcat!)
- Android SingleTask与SingleInstance的区别
Android SingleTask与SingleInstance的区别 原文地址 现有2个项目,taskA.taskB.taskA负责调用taskB中指定的界面. taskB中有3个界面,a.b.c ...
- 【linux】chmod命令详细用法
功能说明: 变更文件或目录的权限 u:User,即文件或目录的拥有者. g:Group,即文件或目录的所属群组. o:Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围. a:Al ...
- 不包含适合于入口点的静态"Main"方法
学习新建项目.此问题做为笔记. 错误 1 程序“admin.exe”不包含适合于入口点的静态“Main”方法 原因:原来创建项目的时候,用的是“空项目”,我以为这样就会生成类库,实际上,一开始准备运行 ...
- android学习笔记27——Activity
Activity配置==> android应用程序要求所有的应用程序组件都需要进行显示配置后,才可正常使用.包括:Activity.Service.BroadCastReceiver.Conte ...
- C3P0数据库连接池的相关bug解决
数据库连接池的几个常见bug: 1.警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@76c7022e -- ...
- 黄聪:VS2010每次F5启动都重新编译但是没办法进入断点
这个问题主要是没有把Debug和Realse的生成配置勾上: