Cully
Cully

Reputation: 470

Using Simple HTML DOM to extract an 'a' URL

I have this code for scraping team names from a table

$url = 'http://fantasy.premierleague.com/my-leagues/303/standings/';
$html = @file_get_html($url);
//Cut out the table
$FullTable = $html->find('table[class=ismStandingsTable]',0);
//get the text from the 3rd cell in the row 
$teamname = $FullTable->find('td',2)->innertext;
echo $teamname;

This much works.. and gives this output....

<a href="/entry/110291/event-history/33/">Why Always Me?</a>

But when I add these lines..

$teamdetails = $teamname->find('a')->href;
echo $teamdetails;

I get completely blank output.

Any idea why? I am trying to get the /entry/110291/event-history/33/ as one variable, and the Why Always Me? as another.

Upvotes: 0

Views: 366

Answers (4)

pguardiario
pguardiario

Reputation: 55002

The problem in your example is that $teamname is a string and you're treating it like a simple_html_dom_node

Upvotes: 0

dave
dave

Reputation: 64707

Marc B is right, I get that you don't have to initialize a variable, but he is saying you are trying to access a property of said variable:

$teamdetails = $teamname->find('a')->href;
           ^^^^^^^^^---- never defined in your code

This is essentially:

$teamname = null;
$teamname->find('a')->href;

Upvotes: 0

JRL
JRL

Reputation: 850

Instead do this:

$tdhtml = DOMDocument::loadHTML($teamdetails);
$link = $tdhtml->getElementsByTagName('a');

$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;

Upvotes: 1

Marc B
Marc B

Reputation: 360872

$teamdetails = $teamname->find('a')->href;
               ^^^^^^^^^---- never defined in your code

I also fail to see how your "works" code could possibly work. You don't define $teamname in there either, so all you'd never get is the output of a null/undefined variable, which is...no output all.

Upvotes: 1

Related Questions