Htmlagilitypack is an open-source parsing of HTML elements of the class library, the biggest feature is that you can use XPath to parse HMTL, if you have previously worked with C # XML, then using the Htmlagilitypack will be handy. Currently the latest version is 1.4.6, as follows: http://htmlagilitypack.codeplex.com/the current stable version is 1.4.6, last update or 2012, so it is stable, basic functions, and there is no need to update.
When it comes to htmlagilitypack, it is necessary to introduce an auxiliary tool that does not know how the page structure is analyzed when others are using it. Anyway, I'm using a tool called Hapexplorer, which is officially provided. Very useful. We'll show you how to use it when we use it.
Or take Liaoning province as an example: http://www.tianqihoubao.com/lishi/ln.htm, open the page, right-click on the page source code, paste into the hapexplorer, you can also open the link directly in Hapexplorer, As the following animation shows:
We can see that the XPath address on the right, the end of the Div, the following is the DL tag, which is the line we are going to collect. Below we use code to get the above structure. First look at the code to get the page source code:
12345678910 |
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;
}
|
public
static
void
ParsePageByArea(String cityCode)
{
//更加链接格式和省份代码构造URL
String url = String.Format(
"http://www.tianqihoubao.com/lishi/{0}.htm"
, cityCode);
//下载网页源代码
var
docText = HtmlHelper.GetWebClient(url);
//加载源代码,获取文档对象
var
doc =
new
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]);
}
}
}
}
Source Address: http://www.cnblogs.com/asxinyu/p/CSharp_HtmlAgilityPack_XPath_Weather_Data.html
c#html Analytical Weapon Htmlagilitypack