Skip to content

Instantly share code, notes, and snippets.

@zhoumengkang
Created December 25, 2013 14:59
Show Gist options
  • Select an option

  • Save zhoumengkang/8123976 to your computer and use it in GitHub Desktop.

Select an option

Save zhoumengkang/8123976 to your computer and use it in GitHub Desktop.
<?php
/**
* zhoumengkang < i@zhoumengkang.com >
* NBA数据采集
*/
set_time_limit(0);
//定义数据库常量(换成你的数据库帐号密码)
define(HOST, 'localhost');
define(USER, 'root');
define(PASSWORD, 'zmkzmk');
$url = 'http://www.stat-nba.com/query_team.php?QueryType=game&order=0&crtcol=tm_out&GameType=season&Team_id=IND&PageNum=30&Season0=2013&Season1=2014';
$baseUrl = 'http://www.stat-nba.com/';
//根据url查看是哪个球队
$teamId = parse_url($url);
parse_str($teamId['query']);
$teamId = $Team_id;//////////////////////////////////////////////////////////////////////////////////teamId
$teamUrl = './team/'.$teamId.'.html';
//获取列表页开始
$res = getContent($url);
preg_match_all('/(game.php\?id=\d{1,})/', $res, $matches);
//连接数据库
$linksArr = $matches[1];
$link = mysql_connect(HOST, USER, PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_set_charset('utf8');
mysql_select_db('nba', $link);
include 'phpQuery.php';
foreach ($linksArr as $key => $value) {
$query = parse_url($linksArr[1]);
parse_str($query['query']);
$matchId = $id; ////////////////////////////////////////////////////////////////////////////////////matchId
phpQuery::newDocumentFile($baseUrl.$linksArr[1]);
//获取比赛时间
$matchTime = pq('.title:eq(0)')->next('div')->html();
preg_match_all('/(\d{4}-\d{1,2}-\d{1,2})/', $matchTime, $matches);
$matchTime = $matches[1][0];///////////////////////////////////////////////////////////////////////matchTime
$teamTitle = pq('.title a');
$teamName = $teamTitle->html();
foreach ($teamTitle as $v) {
//与$teamId核对
if (pq($v)->attr('href')==$teamUrl) {
//整队数据表
$infoTable = pq($v)->parent('.title')->next('div')->next('div')->find('.stat_box')->parent()->html();
//队员的列表数组
$playersArr = pq($infoTable)->find(".sort");
foreach ($playersArr as $kk => $vv) {
//pq($vv)->html();//注释1
/*
<td style="border:0px;"></td>
<td class="normal player_name_out change_color col0 row0"><a href="./player/1342.html" target="_blank">保罗-乔治</a></td>
<td class="current gs change_color col1 row0" rank="1">1</td>
<td class="normal mp change_color col2 row0" rank="39">39</td>
<td class="normal fgper change_color col3 row0" rank="0.473684210526316">47.4%</td>
<td class="normal fg change_color col4 row0" rank="9">9</td>
<td class="normal fga change_color col5 row0" rank="19">19</td>
<td class="normal threepper change_color col6 row0" rank="0.363636363636364">36.4%</td>
<td class="normal threep change_color col7 row0" rank="4">4</td>
<td class="normal threepa change_color col8 row0" rank="11">11</td>
<td class="normal ftper change_color col9 row0" rank="0.769230769230769">76.9%</td>
<td class="normal ft change_color col10 row0" rank="10">10</td>
<td class="normal fta change_color col11 row0" rank="13">13</td>
<td class="normal trb change_color col12 row0" rank="6">6</td>篮板
<td class="normal orb change_color col13 row0" rank="0">0</td>
<td class="normal drb change_color col14 row0" rank="6">6</td>
<td class="normal ast change_color col15 row0" rank="5">5</td>助攻
<td class="normal stl change_color col16 row0" rank="2">2</td>抢断
<td class="normal blk change_color col17 row0" rank="0">0</td>盖帽
<td class="normal tov change_color col18 row0" rank="2">2</td>失误
<td class="normal pf change_color col19 row0" rank="2">2</td>犯规
<td class="normal pts change_color col20 row0" rank="32">32</td>得分
*/
$playerId = pq($vv)->find('.player_name_out a')->attr('href');
$playerId = explode('/', $playerId);
$playerId = trim($playerId[2],'.html');////////////////////////////////////////////////////////////////////////playerId
$playerName = pq($vv)->find('.player_name_out a')->html();///////////////////////////////////////////////////playerName
//投篮命中率
$fgper = pq($vv)->find('.fgper')->html();/////////////////////////////////////////////////////////////////////////fgper
//命中次数
$fg = pq($vv)->find('.fg')->html();//////////////////////////////////////////////////////////////////////////////////fg
//出手次数
$fga = pq($vv)->find('.fga')->html();///////////////////////////////////////////////////////////////////////////////fga
//三分命中率
$threepper = pq($vv)->find('.threepper')->html();/////////////////////////////////////////////////////////////threepper
//三分命中数
$threep = pq($vv)->find('.threep')->html();//////////////////////////////////////////////////////////////////////threep
//三分出手次数
$threepa = pq($vv)->find('.threepa')->html();///////////////////////////////////////////////////////////////////threepa
//罚球命中率
$ftper = pq($vv)->find('.ftper')->html();/////////////////////////////////////////////////////////////////////////ftper
//罚球命中数
$ft = pq($vv)->find('.ft')->html();//////////////////////////////////////////////////////////////////////////////////ft
//罚球数
$fta = pq($vv)->find('.fta')->html();///////////////////////////////////////////////////////////////////////////////fta
//篮板
$trb = pq($vv)->find('.trb')->html();///////////////////////////////////////////////////////////////////////////////trb
//助攻
$ast = pq($vv)->find('.ast')->html();///////////////////////////////////////////////////////////////////////////////ast
//抢断
$stl = pq($vv)->find('.stl')->html();///////////////////////////////////////////////////////////////////////////////stl
//盖帽
$blk = pq($vv)->find('.blk')->html();///////////////////////////////////////////////////////////////////////////////blk
//失误
$tov = pq($vv)->find('.tov')->html();///////////////////////////////////////////////////////////////////////////////tov
//犯规
$pf = pq($vv)->find('.pf')->html();//////////////////////////////////////////////////////////////////////////////////pf
//得分
$pts = pq($vv)->find('.pts')->html();///////////////////////////////////////////////////////////////////////////////pts
//数据库插入操作略了......
}
}
}
}
function getContent($url ){
if(!$url){
die('未给定URL参数.');
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; KB974487)');
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_AUTOREFERER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_ENCODING, ' gzip, deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
$cons = curl_exec($ch);
curl_close($ch);
return $cons;
}
mysql_close($link);
//数据表的创建
/*********************************************************************************************************************************
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE DATABASE IF NOT EXISTS `nba` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `nba`;
DROP TABLE IF EXISTS `match`;
CREATE TABLE IF NOT EXISTS `match` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`match_id` int(11) NOT NULL,
`time` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `players`;
CREATE TABLE IF NOT EXISTS `players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`players_id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `record`;
CREATE TABLE IF NOT EXISTS `record` (
`id` int(11) NOT NULL,
`match_id` int(11) NOT NULL,
`player_id` int(11) NOT NULL,
`fgper` decimal(10,2) NOT NULL,
`fg` int(11) NOT NULL,
`fga` int(11) NOT NULL,
`threepper` decimal(10,2) NOT NULL,
`threep` int(11) NOT NULL,
`threepa` int(11) NOT NULL,
`ftper` decimal(10,2) NOT NULL,
`ft` int(11) NOT NULL,
`fta` int(11) NOT NULL,
`trb` int(11) NOT NULL,
`ast` int(11) NOT NULL,
`stl` int(11) NOT NULL,
`blk` int(11) NOT NULL,
`tov` int(11) NOT NULL,
`pf` int(11) NOT NULL,
`pts` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `team`;
CREATE TABLE IF NOT EXISTS `team` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`team_id` varchar(100) NOT NULL,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
********************************************************************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment