c# - 如何将 HtmlAttribute 转换为字符串?
问题描述
var web = new HtmlWeb();
var doc = web.Load(page);
var Articles = doc.DocumentNode.SelectNodes("//*[@class = 'b-product-grid-tile js-tile-container']");
var href = doc.DocumentNode.SelectNodes("//a[@href]");
foreach (HtmlNode link in href)
{
HtmlAttribute att = link.Attributes["href"];
_entries.Add(new EntryModel { Link = att });
// att.ToString(); <----- Want to convert the HtmlAttribute to a string.
}
我的 Scraper 的完整代码:
入口型号列表:
主窗口:
我需要的链接:
解决方案
最简单的方法是使用System.Web.Mvc.TagBuilder
该类:
var attributes = new { @class = "myClass", id = "elId" };
var tag = new TagBuilder("href");
tag.MergeAttributes(new RouteValueDictionary(attributes));
return tag.ToString();
推荐阅读
- javascript - 调用上下文和词法作用域
- amazon-web-services - AWS 设备场:SetUp 和 TearDown 套件何时以及如何在 devicefarm 的内置测试中工作?
- arrays - 字符串和数组的 Hive 连接
列 - php - PHP:从 url 下载完整的 html 代码(带有封闭标签)
- tensorflow - 调用 TensorFlow Keras 模型时,“training=True”是什么意思?
- google-apps-script - 使用自定义应用脚本功能时如何解决过滤器不匹配问题?
- python - Keras - 如果我想接受三个或更多不同数据大小和类型的输入,这个模型架构是否正确?
- keras - 使用 DNN 时如何处理特征量大但样本量小的数据?
- java - 当edittext不为空时如何自动按回车键?
- javascript - Using regex to find start and end of paragraphs in text file