我尝试使用HTML Agility Pack从html( - > xml)文档(下面)中提取数据。包含“div
”的“id=dealId_*****
”是相关的。当我想出如何用“div
”计算所有“id=dealId_*****
”时,我想我知道如何继续。我尝试使用XPath-Method“starts-with
”,但它不起作用:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(Sourcecode);
int numberOfDIVs;
numberOfDIVs = doc.DocumentNode.SelectNodes("//*[@id='jLocalDeals']/*[starts-with(@id, 'dealId_']").Count;
<div id="jLocalDeals" class="dealsBlock" style="">
<h1>
<div id="dealId_5474417" class="jDeal LEISURE_OFFERS">
<div id="dealId_5476688" class="jDeal SHOPPING">
<div id="dealId_5445019" class="jDeal TICKETS1 RESTAURANT1">
<div class="wrapper3Deals"></div>
<div id="dealId_5474286" class="jDeal BEAUTY">
<div id="dealId_5476685" class="jDeal LEISURE_OFFERS">
<div id="dealId_5474466" class="jDeal SERVICES">
<div class="wrapper3Deals"></div>
<div id="dealId_5466810" class="jDeal BEAUTY">
<div id="dealId_5425417" class="jDeal SERVICES">
<div id="dealId_5474329" class="jDeal SHOPPING">
<div class="wrapper3Deals"></div>
<div id="dealId_5476703" class="jDeal SHOPPING">
<div id="dealId_5476729" class="jDeal SHOPPING">
<div id="dealId_5474702" class="jDeal HEALTHCARE">
<div class="wrapper3Deals"></div>
<div id="dealId_5444044" class="jDeal TRAVEL1" style="display: block;">
<div id="dealId_5474444" class="jDeal LEISURE_OFFERS" style="display: block;">
<div id="dealId_5473774" class="jDeal TRAVEL1" style="display: block;">
<div class="wrapper3Deals"></div>
</div>
P.S。:不幸的是我只能使用.NET 2.0。
答案 0 :(得分:0)
使用contains()函数:
//div[@id='jLocalDeals']//div[contains(@id,'dealId')]
如果需要快捷方式,可以使用xpath中的count()函数:
count(//div[@id='jLocalDeals']//div[contains(@id,'dealId')])
此外,启动的原因 - 在您的xpath中无效是因为您正在检查//div[@id='jLocalDeals']
的子项的ID,而不是<h1>
的子项<div id="dealId_****"/>
1}}。使用starts-with:
//div[@id='jLocalDeals']//div[starts-with(@id,'dealId')]
或者
//div[@id='jLocalDeals']/*/*[starts-with(@id,'dealId')]
希望这会有所帮助,如果我需要澄清任何事情,请告诉我......