2

所以我试图通过 PHP 从另一个网站抓取一些东西并将内容放入 MYSQL。我得到的数据很好,但是一切都乱了套,想不出办法将其插入MYSQL。

这是我获取数据的代码:

<?php
$i = 0;
$homepage = file_get_contents('http://linesfeed.com/nfl');
preg_match_all(
    "'<td style=\'width:auto;color:#424241\' align=\'center\' valign=\'top\'>([^<]*)</td>'si",
    $homepage, $match
);
foreach ($match[1] as $val) {
    $i++;
    $bracktextnumber = '<text' . $i . '>';
    $bracktextnumberclose = '</text' . $i . '>';
    echo $bracktextnumber . $val . $bracktextnumberclose . '<br>';
}
echo "End";
?>

输出如下:

09.05.13
Ravens (Baltimore)
8.0 (-110)
+290
48.5o (-110)
8:35 PM
Broncos (Denver)
-8.0 (-110)
-350
48.5u (-110)
09.08.13
Patriots (NewEngland)
-11.5 (-110)
-630
48.5o (-110)
1:05 PM
Bills (Buffalo)
11.5 (-110)
+465
48.5u (-110)

所以我希望将 text1 (09.05.13) 和 text11 (09.08.13) 作为他们自己的记录输入为 MYSQL 中的日期。将文本 2(乌鸦队)和文本 6(野马队)添加到与文本 1(日期)相同的条目中。文本 12 和文本 17 团队添加到文本 11 日期记录等等。

几件事。从该 URL 加载的游戏数量会有所不同,但位置将始终保持不变。所以就像文本 1 永远是一个日期,文本 2 永远是第一队......

任何帮助将不胜感激!特别是如果提供了示例代码:)

4

1 回答 1

0

马克 B 是正确的。但是,有时你现在只需要一点蛮力。也许 Marc B 可以提供一个真正的 html 解析答案。

$homepage = file_get_contents('http://linesfeed.com/nfl');
preg_match_all("'(<tr)|<td style=\'width:auto;color:#424241\' align=\'center\' valign=\'top\'>([^<]*)</td>'si", $homepage, $match);
$games = trim( implode('|',$match[2]), "| ");
$games = explode('||', str_replace( '||', '|', $games ));
foreach($games as $key => $game)
{
    $gamedata = explode('|', $game );
    $date  = $gamedatas[0];
    $visit = $gamedata[1];
    $home  = $gamedata[6];
//  insert into games (date,visiting_team,home_team) values( $date, $visit, $home ) ...
}

$games 的搜索结果

Array
(
    [0] => 09.05.13|Ravens (Baltimore)|8.0 (-110) |+290|48.5o (-110)|8:35 PM|Broncos (Denver)|-8.0 (-110) |-350|48.5u (-110)
    [1] => 09.08.13|Patriots (NewEngland)|-11.5 (-110) |-630|48.5o (-110)|1:05 PM|Bills (Buffalo)|11.5 (-110) |+465|48.5u (-110)
    [2] => 09.08.13|Titans (Tennessee)|7.0 (-110) |+250|42.0o (-110)|1:05 PM|Steelers (Pittsburgh)|-7.0 (-110) |-300|42.0u (-110)
    [3] => 09.08.13|Falcons (Atlanta)|3.0 (even) |+140|54.5o (-110)|1:05 PM|Saints (NewOrleans)|-3.0 (-120) |-160|54.5u (-110)
    [4] => 09.08.13|Buccaneers (TampaBay)|-3.5 (-110) |-180|40.0o (-110)|1:05 PM|Jets (NewYork)|3.5 (-110) |+160|40.0u (-110)
    [5] => 09.08.13|Chiefs (KansasCity)|-4.0 (-110) |-190|41.0o (-110)|1:05 PM|Jaguars (Jacksonville)|4.0 (-110) |+165|41.0u (-110)
    [6] => 09.08.13|Bengals (Cincinnati)|3.0 (-120) |+130|41.5o (-110)|1:05 PM|Bears (Chicago)|-3.0 (even) |-150|41.5u (-110)
    [7] => 09.08.13|Dolphins (Miami)|0.0 (-110) |OFF|41.0o (-110)|1:05 PM|Browns (Cleveland)|0.0 (-110) |OFF|41.0u (-110)
    [8] => 09.08.13|Seahawks (Seattle)|-3.5 (-115) |-170|45.0o (-110)|1:05 PM|Panthers (Carolina)|3.5 (-105) |+150|45.0u (-110)
    [9] => 09.08.13|Vikings (Minnesota)|5.0 (-110) |+175|46.5o (-110)|1:05 PM|Lions (Detroit)|-5.0 (-110) |-210|46.5u (-110)
    [10] => 09.08.13|Raiders (Oakland)|10.0 (-115) |+350|47.0o (-110)|1:05 PM|Colts (Indianapolis)|-10.0 (-105) |-450|47.0u (-110)
    [11] => 09.08.13|Cardinals (Arizona)|4.5 (-110) |+180|40.0o (-110)|4:30 PM|Rams (StLouis)|-4.5 (-110) |-220|40.0u (-110)
    [12] => 09.08.13|Packers (GreenBay)|4.5 (-110) |+180|48.5o (-110)|4:30 PM|49ers (SanFrancisco)|-4.5 (-110) |-220|48.5u (-110)
    [13] => 09.08.13|Giants (NewYork)|3.0 (-105) |+145|48.5o (-110)|8:35 PM|Cowboys (Dallas)|-3.0 (-115) |-165|48.5u (-110)
    [14] => 09.09.13|Eagles (Philadelphia)|3.5 (-110) |+165|51.0o (-110)|7:15 PM|Redskins (Washington)|-3.5 (-110) |-190|51.0u (-110)
    [15] => 09.09.13|Texans (Houston)|-4.0 (-110) |-200|44.0o (-110)|10:25 PM|Chargers (SanDiego)|4.0 (-110) |+170|44.0u (-110)
)

数据库插入的结果

Date        Visiting Team           Home Team
09.05.13    Ravens (Baltimore)      Broncos (Denver)
09.08.13    Patriots (NewEngland)   Bills (Buffalo)
09.08.13    Titans (Tennessee)      Steelers (Pittsburgh)
09.08.13    Falcons (Atlanta)       Saints (NewOrleans)
09.08.13    Buccaneers (TampaBay)   Jets (NewYork)
09.08.13    Chiefs (KansasCity)     Jaguars (Jacksonville)
09.08.13    Bengals (Cincinnati)    Bears (Chicago)
09.08.13    Dolphins (Miami)        Browns (Cleveland)
09.08.13    Seahawks (Seattle)      Panthers (Carolina)
09.08.13    Vikings (Minnesota)     Lions (Detroit)
09.08.13    Raiders (Oakland)       Colts (Indianapolis)
09.08.13    Cardinals (Arizona)     Rams (StLouis)
09.08.13    Packers (GreenBay)      49ers (SanFrancisco)
09.08.13    Giants (NewYork)        Cowboys (Dallas)
09.09.13    Eagles (Philadelphia)   Redskins (Washington)
09.09.13    Texans (Houston)        Chargers (SanDiego)
于 2013-08-30T18:38:49.243 回答