我在php中创建了一个imdb scrapper。刮板首先废弃了最多'n'部电影和他们在imdb上列出的前15名演员,然后输入任何演员的名字和数字'm'它将显示该演员的顶级'm'电影。 以下是相同的代码
<?php
$temp = fopen ("php://stdin","r");
$limit = fgets($temp);
$num=1;
$url= 'http://www.imdb.com/chart/top';
$doc = new DOMDocument();
//$docmovie = new DOMDocument();
@$doc->loadHTMLFile($url);
$selector = new DOMXPath($doc);
while($num<=$limit)
{
$str = "//*[@id=\"main\"]/div/div[2]/table/tbody/tr[".$num."]/td[2]/a";
$span = $selector->query($str)->item(0);
$movie = $span->nodeValue."<br>";
//echo "<br>".$movie."<br>";
$url_movie = $span->getAttribute('href');
$url_movie = "http://www.imdb.com".str_replace("?ref_=chttp_tt_".$num,"",$url_movie)."fullcredits";
@$doc->loadHTMLFile($url_movie);
$select = new DOMXPath($doc);
$str = "//*[@class=\"cast_list\"]";
$span = $select->query($str)->item(0);
for($x=2;$x<17;$x++)
{
$table = $doc->getElementsByTagName('table')->item(2)->getElementsByTagName('td')->item(2+(4*($x-2)));
$actor = $table->nodeValue."<br>";
if($arr[trim($actor)][0]==NULL)
$arr[trim($actor)][0]=0;
$arr[trim($actor)][++$arr[trim($actor)][0]]=$movie;
//var_dump($arr[$actor]);
}
$num++;
}
var_dump($arr);
if($arr['Morgan Freeman'][0]==NULL)
echo "Actor not in list";
else
{
$limit=$arr['Morgan Freeman'][0];
for($x=1;x<=$limit;$x++)
{
echo $arr['Morgan Freeman'][$x]."<br>";
}
}
?>
这里我输入5作为电影的初始数量,将“The Shawshank Redemption”和“The Dark Knight”作为“Morgan Freeman”的电影。$ arr存储这些值,可以从var_dump的结果中看出( $ ARR);但是当我查询$ arr ['摩根弗里曼']时,它却发出“不在列表中的演员”...... 有人可以帮我理解为什么会这样吗?
var_dump($ arr)的输出是
array(69) { ["Tim Robbins
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Morgan Freeman
"]=> array(3) { [0]=> int(2) [1]=> string(28) "The Shawshank Redemption
" [2]=> string(19) "The Dark Knight
" } ["Bob Gunton
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["William Sadler
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Clancy Brown
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Gil Bellows
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Mark Rolston
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["James Whitmore
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Jeffrey DeMunn
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Larry Brandenburg
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Neil Giuntoli
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Brian Libby
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["David Proval
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Joseph Ragno
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Jude Ciccolella
"]=> array(2) { [0]=> int(1) [1]=> string(28) "The Shawshank Redemption
" } ["Marlon Brando
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Al Pacino
"]=> array(3) { [0]=> int(2) [1]=> string(17) "The Godfather
" [2]=> string(26) "The Godfather: Part II
" } ["James Caan
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Richard S. Castellano
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Robert Duvall
"]=> array(3) { [0]=> int(2) [1]=> string(17) "The Godfather
" [2]=> string(26) "The Godfather: Part II
" } ["Sterling Hayden
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["John Marley
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Richard Conte
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Al Lettieri
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Diane Keaton
"]=> array(3) { [0]=> int(2) [1]=> string(17) "The Godfather
" [2]=> string(26) "The Godfather: Part II
" } ["Abe Vigoda
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Talia Shire
"]=> array(3) { [0]=> int(2) [1]=> string(17) "The Godfather
" [2]=> string(26) "The Godfather: Part II
" } ["Gianni Russo
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["John Cazale
"]=> array(3) { [0]=> int(2) [1]=> string(17) "The Godfather
" [2]=> string(26) "The Godfather: Part II
" } ["Rudy Bond
"]=> array(2) { [0]=> int(1) [1]=> string(17) "The Godfather
" } ["Robert De Niro
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Lee Strasberg
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Michael V. Gazzo
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["G.D. Spradlin
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Richard Bright
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Gastone Moschin
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Tom Rosqui
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Bruno Kirby
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Frank Sivero
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Francesca De Sapio
"]=> array(2) { [0]=> int(1) [1]=> string(26) "The Godfather: Part II
" } ["Christian Bale
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Heath Ledger
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Aaron Eckhart
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Michael Caine
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Maggie Gyllenhaal
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Gary Oldman
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Monique Gabriela Curnen
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Ron Dean
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Cillian Murphy
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Chin Han
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Nestor Carbonell
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Eric Roberts
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Ritchie Coster
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Anthony Michael Hall
"]=> array(2) { [0]=> int(1) [1]=> string(19) "The Dark Knight
" } ["Tim Roth
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Amanda Plummer
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Laura Lovelace
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["John Travolta
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Samuel L. Jackson
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Phil LaMarr
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Frank Whaley
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Burr Steers
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Bruce Willis
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Ving Rhames
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Paul Calderon
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Bronagh Gallagher
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Rosanna Arquette
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Eric Stoltz
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } ["Uma Thurman
"]=> array(2) { [0]=> int(1) [1]=> string(16) "Pulp Fiction
" } }
print_r($ arr)的输出是
Array ( [Tim Robbins
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Morgan Freeman
] => Array ( [0] => 2 [1] => The Shawshank Redemption
[2] => The Dark Knight
) [Bob Gunton
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [William Sadler
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Clancy Brown
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Gil Bellows
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Mark Rolston
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [James Whitmore
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Jeffrey DeMunn
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Larry Brandenburg
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Neil Giuntoli
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Brian Libby
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [David Proval
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Joseph Ragno
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Jude Ciccolella
] => Array ( [0] => 1 [1] => The Shawshank Redemption
) [Marlon Brando
] => Array ( [0] => 1 [1] => The Godfather
) [Al Pacino
] => Array ( [0] => 2 [1] => The Godfather
[2] => The Godfather: Part II
) [James Caan
] => Array ( [0] => 1 [1] => The Godfather
) [Richard S. Castellano
] => Array ( [0] => 1 [1] => The Godfather
) [Robert Duvall
] => Array ( [0] => 2 [1] => The Godfather
[2] => The Godfather: Part II
) [Sterling Hayden
] => Array ( [0] => 1 [1] => The Godfather
) [John Marley
] => Array ( [0] => 1 [1] => The Godfather
) [Richard Conte
] => Array ( [0] => 1 [1] => The Godfather
) [Al Lettieri
] => Array ( [0] => 1 [1] => The Godfather
) [Diane Keaton
] => Array ( [0] => 2 [1] => The Godfather
[2] => The Godfather: Part II
) [Abe Vigoda
] => Array ( [0] => 1 [1] => The Godfather
) [Talia Shire
] => Array ( [0] => 2 [1] => The Godfather
[2] => The Godfather: Part II
) [Gianni Russo
] => Array ( [0] => 1 [1] => The Godfather
) [John Cazale
] => Array ( [0] => 2 [1] => The Godfather
[2] => The Godfather: Part II
) [Rudy Bond
] => Array ( [0] => 1 [1] => The Godfather
) [Robert De Niro
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Lee Strasberg
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Michael V. Gazzo
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [G.D. Spradlin
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Richard Bright
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Gastone Moschin
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Tom Rosqui
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Bruno Kirby
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Frank Sivero
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Francesca De Sapio
] => Array ( [0] => 1 [1] => The Godfather: Part II
) [Christian Bale
] => Array ( [0] => 1 [1] => The Dark Knight
) [Heath Ledger
] => Array ( [0] => 1 [1] => The Dark Knight
) [Aaron Eckhart
] => Array ( [0] => 1 [1] => The Dark Knight
) [Michael Caine
] => Array ( [0] => 1 [1] => The Dark Knight
) [Maggie Gyllenhaal
] => Array ( [0] => 1 [1] => The Dark Knight
) [Gary Oldman
] => Array ( [0] => 1 [1] => The Dark Knight
) [Monique Gabriela Curnen
] => Array ( [0] => 1 [1] => The Dark Knight
) [Ron Dean
] => Array ( [0] => 1 [1] => The Dark Knight
) [Cillian Murphy
] => Array ( [0] => 1 [1] => The Dark Knight
) [Chin Han
] => Array ( [0] => 1 [1] => The Dark Knight
) [Nestor Carbonell
] => Array ( [0] => 1 [1] => The Dark Knight
) [Eric Roberts
] => Array ( [0] => 1 [1] => The Dark Knight
) [Ritchie Coster
] => Array ( [0] => 1 [1] => The Dark Knight
) [Anthony Michael Hall
] => Array ( [0] => 1 [1] => The Dark Knight
) [Tim Roth
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Amanda Plummer
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Laura Lovelace
] => Array ( [0] => 1 [1] => Pulp Fiction
) [John Travolta
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Samuel L. Jackson
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Phil LaMarr
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Frank Whaley
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Burr Steers
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Bruce Willis
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Ving Rhames
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Paul Calderon
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Bronagh Gallagher
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Rosanna Arquette
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Eric Stoltz
] => Array ( [0] => 1 [1] => Pulp Fiction
) [Uma Thurman
] => Array ( [0] => 1 [1] => Pulp Fiction
) )
答案 0 :(得分:0)
我答应检查..所以我检查..我不能使用fopen等因为限制所以我做了一个数组模型就像urs ..有2个名字..我已经改写了一些东西只是为了让你进入正确的方向请cchck http://pastebin.com/uuYCEgYu
继承人代码:
// DEMO ARRAY
$arr = Array();
$arr['Tim Robbins'] = Array ();
array_push($arr['Tim Robbins'],'1','The Shawshank Redemption');
$arr['Morgan Freeman'] = Array ();
array_push($arr['Morgan Freeman'],'2','The Shawshank Redemption', 'The Dark Knight');
//print_r($arr);
//DEMO ARRAY END
//LOOP ARRAY
foreach ($arr as $key=>$data) {
echo $key, "<BR>"; // = actor name
//MATCH ACTOR NAME
if ($key = 'Morgan Freeman') {
echo 'actor found<BR>';
//THERE IS A MATCH SO SHOW DATA
foreach ($arr['Morgan Freeman'] as $data) {
echo $data;
}
} else {
echo 'actor not found<BR>';
}
}