我有一个问题dt,dd从打击 HTML 表中选择所有<dl class="Grid Grid--multicol Grid--2col:40em Grid--4col:60em"> <div class="Grid-cell u-flex u-flexOne u-flexRow"> <dt> Item ID:</dt> <dd class="u-weightSemibold u-paddingL5px">72547664</dd> </div> <div class="Grid-cell u-flex u-flexOne u-flexRow"> <dt> License:</dt> <dd class="u-weightSemibold u-paddingL5px"> <a class="u-linkDodgerBlue js-infoLicenseString" href="" target="_blank">Standard</a> </dd> </div> <div class="Grid-cell u-flex u-flexOne u-flexRow"> <dt> Usage:</dt> <dd class="u-weightSemibold">Commercial<span class="TooltipQuestionMark"></span></dd> </div> <div class="Grid-cell u-flex u-flexOne u-flexRow"> <dt> Required Plugins:</dt> <dd class="u-weightSemibold u-paddingL5px">None</dd> </div> <div class="Grid-cell u-flex u-flexOne u-flexRow"> <dt> Creation Tool:</dt> <dd class="u-weightSemibold u-paddingL5px">Autodesk</dd> </div> <div class="Grid-cell u-flex u-flexOne u-isHidden:0-60em"></div></dl>我写这个 i = 0; doc.DocumentNode.SelectNodes("//dl").Where(x => x.HasClass("Grid")).FirstOrDefault().SelectNodes(".//div").ToList().ForEach(x => { result.Items.Add(new DownloadItem { Name = "specs-title", Index = i, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dt").FirstOrDefault().GetText() }); result.Items.Add(new DownloadItem { Name = "specs", Index = i++, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dd").GetText() }); });上面的代码只是抓取第一行 Item ID:| 72547664 什么我的问题,列出所有dt与dd信息
1 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
你的代码的问题是,当你第一次想dl用 class获取所有元素时Grid,你只用方法获取那种类型的 DOM 元素中的第一个FirstOrDefault()。
i = 0; doc.DocumentNode.SelectNodes("//dl").Where(x => x.HasClass("Grid")).SelectNodes(".//div").ToList().ForEach(x =>
{
result.Items.Add(new DownloadItem { Name = "specs-title", Index = i, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dt").FirstOrDefault().GetText() });
result.Items.Add(new DownloadItem { Name = "specs", Index = i++, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dd").GetText() });
});
您必须删除FirstOrDefault()以获取适合您需求的所有元素。
- 1 回答
- 0 关注
- 162 浏览
添加回答
举报
0/150
提交
取消