0
votes

I want to scrape data with Html Agility Pack. I used this:

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++;
}

The XPath is taken from FireXPath but nothing appears.

The HTML snippet is this:

<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>

Could anyone help me find the correct XPath? I used this technique in other sites and I had taken the results I wanted but from this site I have some problem to find the correct XPath.

1
What data/element are you trying to get? - krlzlx
i want to take the name of teams and the odds . i thing this : <div class="ippg-Market_CompetitorName"> - ggeorge
From the HTML snippet you provided or from the URL bet365? - krlzlx
from bet365 is difficult for me because uses javascript and for this reason i try to parse data from their mobile version. So i think is better from the snippet i post - ggeorge

1 Answers

0
votes

You can get your teams and odds from the HTML snippet like this:

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);
}