Jump to content

Scrape div from site and echo with php


adamci24

Recommended Posts

Hey guys, I am not great with PHP and am looking for some help getting a script that pulls Div info from a page and echos it as an xml/rss feed. This is for fantasy football, and the code below is an excerpt from the main page taht only shows 1 of 4 matchups for the week. I need to pull the team name and the corresponding score and then pull the opponents name and their score with "VS" text in between them in the final echo line for my rss. So I am looking for the final echo to be something like this example "Doug Flutie 0.00 VS Hellraiser 0.00"

 

Below you will see the current scores (<div class='Fz-lg'>0.00</div>) for both teams which is 0.00 currently. You will also see the team name (<div class='Fz-sm Phone-fz-xs Ell Mawpx-175'><a class="F-link" href="/f1/793293/3">Doug Flutie</a></div>). The one issue I'm seeing is that each teams score uses the same div id of "Fz-lg"....

 

The team name div is unique where there is a number at the end....

 

<div class='Fz-sm Phone-fz-xs Ell Mawpx-175'><a class="F-link" href="/f1/793293/3">Doug Flutie</a></div>

 

As you can see team "Doug Flutie" is team known as team 3 by "/f1/793293/3"...the last number is 3. Team Hellraisers is team 7 if you look at the code below. 

 

Like I said, this is just one of 4 matchups on the site, so this block of code below repeats 3 more times in the same format. The only thing that changes is the last digit for the team name. If I could get this to work id love to have all 4 matchups echoed with the current scores and names. Can anyone help me out with this? Id really appreciate it. Thanks!

 <div class="Bd No-p Js-submods Tst-matchups-body">
      <section class="No-m No-pend Submod Selected">
        <p class='Ta-c Fz-xs Pstart-xl Py-med Mbot-sm'>Week 1 - <span class=''>Pre-Game</span></p><ul class="List-rich"><li class='Linkable Listitem No-p ' data-linkable='true' data-target='/f1/793293/matchup?week=1&mid1=3&mid2=7'>
          <div class='Ta-c Js-hidden'><a href='/f1/793293/matchup?week=1&mid1=3&mid2=7'>View Matchup</a></div>
          <div class='Grid-table Phone-px-med'>

            <div class='Grid-u-1-2 Py-med  Bdrend'>
              <div class='Grid-bind-end Grid-h-mid Nowrap'>

                <div class='Grid-u-1-4 Phone-grid-u-1-3'>
                  <div class='Ta-end Pend-xl Phone-ta-c Phone-no-p'>
                      <div class='Fz-lg'>0.00</div>  <div class='F-shade  '>173.33</div>
                  </div>
                </div>

                <div class='Grid-u Phone-grid-u-2-3'>
                  <div class='Ta-end Grid-h-top Mstart-sm Phone-ta-start Phone-no-mstart'>
                    <div class='Grid-u Va-top Pend-lg Ptop-sm Phone-grid-u-4-5 Phone-no-py Phone-ta-end'>
                          <div class='Fz-sm Phone-fz-xs Ell Mawpx-175'><a class="F-link" href="/f1/793293/3">Doug Flutie</a></div>
                          <div>0-0-0 </div>
                        </div>
                    <span class='Grid-u Pend-lg Phone-grid-u-1-5 Phone-no-px Phone-ptop-med'><a class='Grid-u' href='/f1/793293/3'><img class="Avatar-med Grid-u Mend-med" src="https://i.imgur-ysports.com/CigQjS2s.jpg" alt="avatar"> </a></span>
                  </div>
                </div>

              </div>
            </div>
            <div class='Grid-u-1-2 Py-med'>
              <div class='Grid-bind-start Grid-h-mid Nowrap'>

                <div class='Grid-u-1-4 Phone-grid-u-1-3'>
                  <div class='Pstart-xl Ta-start Phone-ta-c Phone-no-p'>
                      <div class='Fz-lg'>0.00</div>  <div class='F-shade  '>140.67</div>
                  </div>
                </div>

                <div class='Grid-u Phone-grid-u-2-3'>
                  <div class='Grid-h-top Ta-start Mend-sm Phone-no-mend'>
                    <span class='Grid-u Fl-start Phone-grid-u-1-5 Phone-ptop-med'><a class='Grid-u' href='/f1/793293/7'><img class="Avatar-med Grid-u Mstart-med" src="https://s.yimg.com/dh/ap/fantasy/nfl/img/icon_04_100.png" alt="avatar"> </a></span>
                    <div class='Grid-u Va-top Pstart-lg Ptop-sm Phone-grid-u-4-5 Phone-no-py'>
                          <div class='Fz-sm Phone-fz-xs Ell Mawpx-175'><a class="F-link" href="/f1/793293/7">Hellraisers</a></div>
                          <div>0-0-0 </div>
                        </div> 
                  </div>
                </div>

              </div>
            </div>
          </div>

Edited by adamci24
Link to comment
Share on other sites

Looking at the HTML you provided each team info is contained in these parent divs  <div class='Grid-bind-start Grid-h-mid Nowrap'>  and  <div class='Grid-bind-end Grid-h-mid Nowrap'>  I would use xpath to get the contents of these divs. Once you have the team info you would store each team info in an array. Then use array_chunk to pair up the teams, after loop over the paired teams array to output team vs team.

$doc = new DOMDocument();
$doc->loadHTML($url);

$xpath = new DOMXPath($doc);

// return the content of these divs
//  <div class='Grid-bind-start Grid-h-mid Nowrap'>
//  <div class='Grid-bind-end Grid-h-mid Nowrap'>
//  these are the parent divs that contain the team score and name along with other information
$query = "//div[starts-with(@class, 'Grid-bind-') and contains(@class, 'Grid-h-mid Nowrap')]";

$entries = $xpath->query($query);

$teams = array();
foreach ($entries as $entry)
{
    // remove whitespace and replace with a comma
    $score_team = preg_replace('~\s{2,}~', ',',  trim($entry->nodeValue));

    // the team score, and name with outher info is now seperated by a comma
    // use explode to split the data into an array
    // the team score will be the first item
    // the team name is the third item
    list($score, $team,) = explode(',', $score_team);

    // add team name and score to a teams array
    $teams[] = $team . ' ' . $score;
}

// pair up the teams in the teams array
$pairedTeams = array_chunk($teams, 2);

// output team VS team
foreach ($pairedTeams as $teams)
{
    echo implode(' VS ', $teams) . '<br />';
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.