刚做了一个小任务,需要抓取其他网站的部分数据,这里就顺便介绍使用Winista.Text.HtmlParser这个类库如何解析HTML并抓取部分数据
1、获取指定网站的页面源码
string url = "http://www.100njz.com/price/list/p--------1.html";
System.Net.WebClient aWebClient = new System.Net.WebClient();
aWebClient.Encoding = System.Text.Encoding.Default;
string html = aWebClient.DownloadString(url);
2、获取到源码后,解析并获取指定的节点数据,这里演示的是获取id="articleList"的div
Lexer lexer = new Lexer(html);
Parser parser = new Parser(lexer);
NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.Div));
NodeList nodeList = parser.Parse(filter);
ITag t;
if (nodeList.Count == 0)
Response.Write("没有符合要求的节点");
else
{
for (int i = 0; i < nodeList.Count; i++)
{
t = getTag(nodeList[i]);
if (t != null && t.GetAttribute("id") == "articleList")
{
NodeFilter filter2 = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.LinkTag));
Response.Write(nodeList[i].ToHtml());
}
}
}
private ITag getTag(INode node)
{
if (node == null)
return null;
return node is Div ? node as Div : null;
}