[英]Extracting string from Html page using C#
我有一个源html页面,我想执行以下操作:
如果有人可以帮助我,我会非常感激和感激,因为我对使用C#并不了解。
您可以使用以下代码:
HttpClient http = new HttpClient();
//I have put Ebay.com. you could use any.
var response = await http.GetByteArrayAsync("ebay.com");
String source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
source = WebUtility.HtmlDecode(source);
HtmlDocument Nodes = new HtmlDocument();
Nodes.LoadHtml(source);
在Nodes对象中, all the DOM elements in the HTML page
中将具有all the DOM elements in the HTML page
。
您可以使用linq过滤掉所需的内容。
范例:
List<HtmlNode> RequiredNodes = Nodes.DocumentNode.Descendants()
.Where(x => x.Attributes["Class"].Contains("List-Item")).ToList();
您可能需要安装Html Agility Pack NuGet或从链接中下载它。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.