HtmlAgilityPack是一个开源的解析HTML元素的类库,最大的特点是可以通过XPath来解析HMTL,如果您以前用C#操作过XML,那么使用起HtmlAgilityPack也会得心应手。目前最新版本为1.4.6。

程序示例如下:

代码如下:

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WinformHtmlAgilityPack
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(DownloadData);
thread.Start();
}
private void DownloadData()
{
GZipWebClient c = new GZipWebClient();
List<HouseModel> houseList = new List<HouseModel>();
int pageCount = 50;
int index = 1;
for (int m = 1; m <= pageCount; m++)
{
byte[] data = c.DownloadData(new Uri("http://newhouse.fang.com/house/s/b9" + m + "/"));
string strx = Encoding.Default.GetString(data); HtmlWeb htmlWeb = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(strx); //HtmlNode node = htmlDoc.GetElementbyId("sjina_C01_37");
//HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='nl_con clearfix']");
//HtmlNode list = node.SelectSingleNode("//ul"); //根据xpath ul下的li li[2]代表索引 得到 名称
//HtmlNode nodeli_name = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='bx1']/div/div[1]/div[1]/div/div/ul/li[1]/div/div[2]/div[1]/div[1]/a");
//根据xpath ul下的li li[2]代表索引 得到 价格
//HtmlNode nodeli_price = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='bx1']/div/div[1]/div[1]/div/div/ul/li[3]/div/div[2]/div[4]/span");
for (int i = 1; i <= 20; i++)
{
//通过Xpath选中html的指定元素 HouseModel houseModel = new HouseModel();
HtmlNode nodeli_name = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='bx1']/div/div[1]/div[1]/div/div/ul/li[" + i + "]/div/div[2]/div[1]/div[1]/a");
if (nodeli_name != null)
{
houseModel.楼盘名称 = NoHTML(nodeli_name.InnerHtml);
}
HtmlNode nodeli_price = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='bx1']/div/div[1]/div[1]/div/div/ul/li[" + i + "]/div/div[2]/div[4]/span");
if (nodeli_price != null)
{
houseModel.价格 = NoHTML(nodeli_price.InnerHtml);
}
HtmlNode nodeli_unit = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='bx1']/div/div[1]/div[1]/div/div/ul/li[" + i + "]/div/div[2]/div[4]/em");
if (nodeli_unit != null)
{
houseModel.单位 = NoHTML(nodeli_unit.InnerHtml);
} HtmlNode nodeli_address = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='bx1']/div/div[1]/div[1]/div/div/ul/li[" + i + "]/div/div[2]/div[2]/div[1]/a");
if (nodeli_address != null)
{
string district = nodeli_address.InnerHtml.Split(']')[0];
houseModel.所在区域 = NoHTML(district).Substring(1);
houseModel.地址 = NoHTML(nodeli_address.InnerHtml);
}
houseModel.序号 = index;
houseList.Add(houseModel);
this.label1.Invoke(new MethodInvoker(delegate
{
this.label1.Text = "已经抓取:"+houseList.Count.ToString()+" 条....."; }));
index++; this.dataGridView1.Invoke(new MethodInvoker(delegate
{
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = houseList;
this.dataGridView1.Columns[5].Width = 500; this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;
})); Thread.Sleep(100);
}
}
this.label1.Invoke(new MethodInvoker(delegate
{
this.label1.Text = "已经抓取完毕。"; }));
}
public class GZipWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest webrequest = (HttpWebRequest)base.GetWebRequest(address);
webrequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
return webrequest;
}
}
public class HouseModel
{
public int 序号 { get; set; }
public string 楼盘名称 { get; set; }
public string 所在区域 { get; set; }
public string 价格 { get; set; }
public string 单位 { get; set; }
public string 地址 { get; set; }
} private void btn_exportexcel_Click(object sender, EventArgs e)
{
DataGridViewToExcel(this.dataGridView1);
}
#region
private void DataGridViewToExcel(DataGridView dgv)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Execl files (*.xls)|*.xls";
dlg.FilterIndex = 0;
dlg.RestoreDirectory = true;
dlg.CreatePrompt = true;
dlg.Title = "保存为Excel文件"; if (dlg.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = dlg.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string columnTitle = "";
try
{
//写入列标题
for (int i = 0; i < dgv.ColumnCount; i++)
{
if (i > 0)
{
columnTitle += "\t";
}
columnTitle += dgv.Columns[i].HeaderText;
}
sw.WriteLine(columnTitle); //写入列内容
for (int j = 0; j < dgv.Rows.Count; j++)
{
string columnValue = "";
for (int k = 0; k < dgv.Columns.Count; k++)
{
if (k > 0)
{
columnValue += "\t";
}
if (dgv.Rows[j].Cells[k].Value == null)
columnValue += "";
else
columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
}
sw.WriteLine(columnValue);
}
sw.Close();
myStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
#endregion
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
return Htmlstring;
}
}
}

 源码地址:http://files.cnblogs.com/files/sunyj/WinformHtmlAgilityPack.rar

HtmlAgilityPack抓取搜房网数据简单示例的更多相关文章

  1. [Python爬虫] 之二十五:Selenium +phantomjs 利用 pyquery抓取今日头条网数据

    一.介绍 本例子用Selenium +phantomjs爬取今日头条(http://www.toutiao.com/search/?keyword=电视)的资讯信息,输入给定关键字抓取资讯信息. 给定 ...

  2. [Python爬虫] 之十九:Selenium +phantomjs 利用 pyquery抓取超级TV网数据

    一.介绍 本例子用Selenium +phantomjs爬取超级TV(http://www.chaojitv.com/news/index.html)的资讯信息,输入给定关键字抓取资讯信息. 给定关键 ...

  3. Httpclient: 多层翻页网络爬虫实战(以搜房网为例)

    参考:http://blog.csdn.net/qy20115549/article/details/52912532 一.创建数据表 #创建表:用来存储url地址信息 create table so ...

  4. 使用 Python 抓取欧洲足球联赛数据

    Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤    数据的采集和获取    数据的清洗,抽取,变形和装载    数据的分析,探索和预测    ...

  5. 抓取Js动态生成数据且以滚动页面方式分页的网页

    代码也可以从我的开源项目HtmlExtractor中获取. 当我们在进行数据抓取的时候,如果目标网站是以Js的方式动态生成数据且以滚动页面的方式进行分页,那么我们该如何抓取呢? 如类似今日头条这样的网 ...

  6. 如何用python抓取js生成的数据 - SegmentFault

    如何用python抓取js生成的数据 - SegmentFault 如何用python抓取js生成的数据 1赞 踩 收藏 想写一个爬虫,但是需要抓去的的数据是js生成的,在源代码里看不到,要怎么才能抓 ...

  7. Python实例之抓取淘宝商品数据(json型数据)并保存为TXT

    本实例实现了抓取淘宝网中以‘python’为关键字的搜索结果,经详细查看数据存储于html文档中的js脚本中,数据类型为JSON 具体实现代码如下: import requests import re ...

  8. Python实例之抓取HTML中的数据并保存为TXT

    本实例实现了抓取捧腹网中存储于html中的笑话数据(非JSON数据) 通过浏览器相关工具发现捧腹网笑话页面的数据存储在HTML页面而非json数据中,因此可以直接使用soup.select()方法来抓 ...

  9. HtmlAgilityPack 抓取页面的乱码处理

    HtmlAgilityPack 抓取页面的乱码处理 用来解析 HTML 确实方便.不过直接读取网页时会出现乱码. 实际上,它是能正确读到有关字符集的信息,怎么会在输出时,没有取到正确内容. 因此,读两 ...

随机推荐

  1. ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】

    Street Lamps   Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...

  2. Code[VS]1021 玛丽卡题解

    Code[VS]1021 玛丽卡题解 SPFA Algorithm 题目传送门:http://codevs.cn/problem/1021/ 题目描述 Description 麦克找了个新女朋友,玛丽 ...

  3. 【JAVA】JMX简单使用方法

    [BEAN] 配置   <!-- JMX 对应的接口服务--> <bean id="emailInterfaceServer" class="com.s ...

  4. strace命令跟踪进程

    在实际系统维护过程中,常常需要知道一个进程在做哪些动作,比如想判断一个进程是否hang,我们可以使用strace命令,此命令式用来跟踪一个进程在调用哪些系统函数和信号 通过跟踪xinetd进程演示st ...

  5. Timestame类型和String 类型的转化

    Timestame类型和String 类型的转化 String转化为Timestamp: SimpleDateFormat df = new SimpleDateFormat("yyyy-M ...

  6. The Beatles-Hey Jude

    轉載自 https://www.youtube.com/watch?v=V3jCYm_QGZQ Hey Jude, don't make it bad.Take a sad song and make ...

  7. Cento 安装配置FastFDS

    unzip -x libfastcommon-master.zip ./make.sh ./make.sh install /usr/include/fastcommon cd FastDFS ./m ...

  8. Centos 开放80端口

    一.添加规则 #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #/sbin/iptables -I INPUT -p tcp --dport ...

  9. 8个主要的Velocity语法使用说明

    8个主要的Velocity语法使用说明,分别是:Velocity表达式,Velocity注释,Velocity循环,Velocity条件判断,Velocity赋值,Velocity调试,Velocit ...

  10. PHP 操作MySQL———来自copy

    学习要点:1.PHP 连接到MySQL2.增删改查3.其他常用函数 如果你已经具有了使用PHP.SQL 和MySQL 的丰富经验,现在就可以把所有这些技术组合在一起.PHP 与MySQL 之间稳固的集 ...