FireXPath的XPath不适用于Html Agility Pack

时间:2017-09-29 08:34:32

标签: c# xpath html-parsing html-agility-pack

我想用Html Agility Pack抓取数据。 我用过这个:

string url = @"https://mobile.bet365.gr/#type=Coupon;key=1-1-13-40-141-0-0-0-1-0-0-4100-0-0-1-0-0-0-0-0-0;ip=0;lng=5;anim=1";
var webGet = new HtmlWeb();
var document = webGet.Load(url);
var nodes = document.DocumentNode.SelectNodes("//*[@id='Coupon']/div[1]/div[2]/div[1]/div/div[1]/div[1]/span");

int i = 0;

foreach (var node in nodes)
{
    dataGridView1.Rows.Add();
    dataGridView1.Rows[i].Cells[0].Value = i + 1;
    dataGridView1.Rows[i].Cells[1].Value = node.InnerHtml;
    i++;
}

XPath取自FireXPath,但没有出现。

HTML代码段是:



<div id="Coupon" class="C4 C4_1">
    <div class="liveAlertKey enhancedPod cc_12_7" data-sportskey="1-1-13-40-141-0-0-0-1-0-0-4100-0-0-1-0-0-0-0-0-0" data-alertkey="NPower Champs">
        <h1><em>Αγγλία - Τσάμπιονσιπ</em></h1>
        <div class="podHeaderRow">
            <div class="wideLeftColumn">Παρ 29 Σεπ</div>
            <div class="priceColumn"><em>1</em></div>
            <div class="priceColumn"><em>X</em></div>
            <div class="priceColumn"><em>2</em></div>
        </div>
        <div data-fixtureid="67185688" data-plbtid="40" class="podEventRow cc_12_4 ippg-Market " data-nav="rw_spl_sc_1-1-8-67185688-3-0-0-0-1-0-0-0-0-0-1-0-0-0-0-0-0,MarketCount,1-1-8-67185688-3-0-0-0-1-0-0-0-0-0-1-0-0-0-0-0-0,False,1">
            <div class="wideLeftColumn hasStatsIcon">
                <div class="ippg-Market_GameDetail">
                    <div class="ippg-Market_GameItem ">
                        <div class="ippg-Market_CompetitorName">
                            <span class="ippg-Market_Truncator">ΚΠΡ</span>
                        </div>
                        <div class="ippg-Market_CompetitorScores">
                            <span class="ippg-PointNode"></span>
                        </div>
                    </div>
                    <div class="ippg-Market_GameItem ">
                        <div class="ippg-Market_CompetitorName">
                            <span class="ippg-Market_Truncator">Φούλαμ</span>
                        </div>
                        <div class="ippg-Market_CompetitorScores">
                            <span class="ippg-PointNode"></span>
                        </div>
                    </div>
                    <div class="ippg-Market_MetaContainer ">
                        <div class="ippg-Market_GameStartTime">20:45</div>
                        <div class="ippg-Market_GameInfo "></div>
                        <div class="ippg-Market_MarketCount">109</div>
                        <div id="FixtureIconsContainer">
                            <img src="/grfx/V6/Misc/pixel.gif" class="VideoIcon SSP-7">
                        </div>
                        <div id="StatsIconContainer">
                            <a class="icon-stats" target="_blank" data-nav="externalLink" href="http://www.stats.betradar.com/s4/?clientid=259&amp;matchid=11868244&amp;language=el"></a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="ippg-Market_Topic priceColumn" data-nav="pt=N#o=9/4#f=67185688#fp=1410316836#so=0#c=1#" data-inplaytopic="" data-pgfpid="1410316836" data-inplaymarkettopic="" data-inplayaltmarkettopic="">
                <span class="ippg-Market_Odds">3.25</span>
            </div>
            <div class="ippg-Market_Topic priceColumn" data-nav="pt=N#o=13/5#f=67185688#fp=1410316839#so=0#c=1#" data-inplaytopic="" data-pgfpid="1410316839" data-inplaymarkettopic="" data-inplayaltmarkettopic="">
                <span class="ippg-Market_Odds">3.60</span>
            </div>
            <div class="ippg-Market_Topic priceColumn" data-nav="pt=N#o=5/4#f=67185688#fp=1410316841#so=0#c=1#" data-inplaytopic="" data-pgfpid="1410316841" data-inplaymarkettopic="" data-inplayaltmarkettopic="">
                <span class="ippg-Market_Odds">2.25</span>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

有人能帮我找到正确的XPath吗?我在其他网站上使用了这种技术,并且我已经获得了我想要的结果,但是从这个网站我找到正确的XPath有一些问题。

1 个答案:

答案 0 :(得分:0)

您可以从HTML代码段中获取您的团队和赔率:

HtmlDocument document = new HtmlDocument();
document.Load(Server.MapPath("xpath.html"));

// Teams
HtmlNodeCollection teamNodes = document.DocumentNode.SelectNodes("//div[@class='ippg-Market_CompetitorName']");

List<string> teams = new List<string>();

foreach (HtmlNode n in teamNodes)
{
    HtmlNode nodeTeam = n.SelectSingleNode(".//span[@class='ippg-Market_Truncator']");

    if (nodeTeam != null)
    {
        teams.Add(nodeTeam.InnerText);
    }
}

// Odds
HtmlNodeCollection oddNodes = document.DocumentNode.SelectNodes("//span[@class='ippg-Market_Odds']");

List<string> odds = new List<string>();

foreach (HtmlNode o in oddNodes)
{
    odds.Add(o.InnerText);
}