0

http://www.nfl.com/widget/gc/2011/tabs/cat-post-boxscore?gameId=2012093000

我希望从上面链接之类的页面中抓取数据(即游戏级别的 NFL 数据)。

NFL.com 有一个方便的 JSON API,可以让大量此类数据可访问。也就是说,对于 2010 年及以后的游戏。对于早期的游戏,我将不得不解析类似于上述页面的 HTML。

我一直在尝试使用 Xpath 来抓取它。但是,我发现很难区分作为“thd2”类表行的表头和作为“tbdy1”类表行的数据

如果有人知道如何遍历这些数据并提取数据、表头并将它们放入一个数组中,我想看看你的方法!

$curl = curl_init('http://www.nfl.com/widget/gc/2011/tabs/cat-post-boxscore?gameId=2012093000');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10');

$html = curl_exec($curl);
curl_close($curl);

$dom = new DOMDocument();
@$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$tables = $xpath->query('//table[1]/tbody/td');
var_dump($tables);
4

0 回答 0