C#解析HTML网页
如何用C解析一个完整的HTML网站# 小例子C#解析HTML网页,c#,wpf,C#,Wpf,如何用C解析一个完整的HTML网站# 小例子 <html> <head></head> <body> <div class="wrapper"> <div class="row"> <div>Value1</div> <div>Value2</div> </div> <div class="row"> &
<html>
<head></head>
<body>
<div class="wrapper">
<div class="row">
<div>Value1</div>
<div>Value2</div>
</div>
<div class="row">
<div>Value1</div>
<div>Value2</div>
</div>
<div class="row">
<div>Value1</div>
<div>Value2</div>
</div>
<div class="row">
<div>Value1</div>
<div>Value2</div>
</div>
</div>
</body>
</html>
在断点出现之前,一切正常,但在出现断点之前,我不知道该如何继续。您可以使用提取数据。它的用法类似于parse libs。对于您的html示例,它类似于:
IEnumerable<WrapperItem> items = WebContentParser.ParseList<WrapperItem>(html);
// ...
[ListSelector(".wrapper", ChildSelector = ".row")]
class WrapperItem
{
[Selector("div:nth-child(1)")]
public string Value1 { get; set; }
[Selector("div:nth-child(2)")]
public string Value2 { get; set; }
}
取决于HTML HTML的格式不是很好的XML,应该考虑使用解析。
IEnumerable<WrapperItem> items = WebContentParser.ParseList<WrapperItem>(html);
// ...
[ListSelector(".wrapper", ChildSelector = ".row")]
class WrapperItem
{
[Selector("div:nth-child(1)")]
public string Value1 { get; set; }
[Selector("div:nth-child(2)")]
public string Value2 { get; set; }
}
WebClient client = new WebClient ();
string html = client.DownloadString("https://example.com");