Un selecteur correct mais une selection raté ?
Posté : 07 juin 2021, 10:49
Et bien bonjour à vous ! C'est la première fois que j'écris sur un forum (qui plus est, pour du code) donc je ne suis pas sûr de la section, mais j'imagine qu'on corrigera au besoin.
Donc, rentrons dans le vif du sujet !
Je précise que je travaille avec la librairie Goutte dans mon programme et que j'ai commencé à toucher au PHP il y a 3 semaines.
J'ai, grosso modo, voulu récupérer différentes données se trouvant sur pas loin de 220 pages. (Pour ceux qui connaissent, Persona et donc les différentes stats ect. de chaque Persona, et j'ai commencé avec Persona 4 et Persona 4 Golden qui sont sensiblement les mêmes).
Accéder aux différentes pages, pas un soucis. Récupérer les données, pas un souci non plus. Mais il y a une chose qui ne semble pas vouloir fonctionner.
Pour détailler, j'ai cette Page ( https://megamitensei.fandom.com/wiki/Su ... a_4_Golden ) qui contient donc un tableau que je souhaite récupérer (Celui en jaune avec Persona 4 et Persona 4 Golden).
Puisqu'il y a des informations différentes pour les deux, je vais donc souhaiter récupérer les deux.
Pour le premier, sachant que je récupère uniquement les informations de la div "Base" (qui peut également s'appeler Evolve, mais correspond toujours à la première div), j'obtiens ce que je souhaite.
Pour le second, P4G, il semble incapable de le trouver malgré le sélecteur que j'ai revérifié, est correct, et correspond au premier try, et le fait qu'il effectue bel et bien les différents essais.
Voilà, je pense avoir tout dis. Je vous donne le code plus bas (Oui, les try catchs qui s'enchaînent sont vraiment horrible à voir. Mais je n'ai pas trouvé d'autres solutions étant donné les différentes possibilités et les irrégularités du site pour les différentes tables que je vais chercher)
Merci d'avance pour votre aide et merci de m'avoir lu !
Donc, rentrons dans le vif du sujet !
Je précise que je travaille avec la librairie Goutte dans mon programme et que j'ai commencé à toucher au PHP il y a 3 semaines.
J'ai, grosso modo, voulu récupérer différentes données se trouvant sur pas loin de 220 pages. (Pour ceux qui connaissent, Persona et donc les différentes stats ect. de chaque Persona, et j'ai commencé avec Persona 4 et Persona 4 Golden qui sont sensiblement les mêmes).
Accéder aux différentes pages, pas un soucis. Récupérer les données, pas un souci non plus. Mais il y a une chose qui ne semble pas vouloir fonctionner.
Pour détailler, j'ai cette Page ( https://megamitensei.fandom.com/wiki/Su ... a_4_Golden ) qui contient donc un tableau que je souhaite récupérer (Celui en jaune avec Persona 4 et Persona 4 Golden).
Puisqu'il y a des informations différentes pour les deux, je vais donc souhaiter récupérer les deux.
Pour le premier, sachant que je récupère uniquement les informations de la div "Base" (qui peut également s'appeler Evolve, mais correspond toujours à la première div), j'obtiens ce que je souhaite.
Pour le second, P4G, il semble incapable de le trouver malgré le sélecteur que j'ai revérifié, est correct, et correspond au premier try, et le fait qu'il effectue bel et bien les différents essais.
Voilà, je pense avoir tout dis. Je vous donne le code plus bas (Oui, les try catchs qui s'enchaînent sont vraiment horrible à voir. Mais je n'ai pas trouvé d'autres solutions étant donné les différentes possibilités et les irrégularités du site pour les différentes tables que je vais chercher)
Merci d'avance pour votre aide et merci de m'avoir lu !
<?php
//NOTE : toutes les way sont en XPath car CSS limité
require __DIR__ . '/../../users/Killian/vendor/autoload.php';
use Goutte\Client;
function personaread($personaurl, $name)
{
$client = new Client();
$crawler = $client->request('GET', $personaurl);
$crawlerP4G = $crawler;
//selecteur Xpath=/span[@id="Persona_4"]/parent::h3/following::table[1]
//Selecteur CSS
/*if golden exist
if base exist div[title*=base]
else div[title=Persona_4]
else table[style*="2px solid #FFE600"]
*/
$personaname = ($crawler->filter('[id="firstHeading"]')->text());
//Try catch for id Persona_4 & Persona_4_Golden + if div base/level/evolve
if ($name == "Izanagi-no-Okami") {
$table = $crawler->filterXPath('//span[@id="Persona_4_3"]/parent::h3/following::table[1]');
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} else {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_2"]/div[1]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_2"]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_2"]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//span[@id="Persona_4_2"]/parent::h3/following::table[1]');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4"]/div[1]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4"]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4"]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//span[@id="Persona_4"]/parent::h3/following::table[1]');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_Golden"]/div[1]/div[1]/table');
$P4GO = TRUE;
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_Golden"]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_Golden"]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_Golden_2"]/div[1]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_Golden_2"]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Persona_4_Golden_2"]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//span[@id="Persona_4_Golden_2"]/parent::h3/following::table[1]');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Golden"]/div[1]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Golden"]/div[1]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//div[@data-hash="Golden"]/table');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//span[@id="Golden"]/parent::h3/following::table[1]');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table = $crawler->filterXPath('//span[@id="Persona_4_Golden"]/parent::h3/following::table[1]');
try {
$arcanaP4 = $table->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
echo 'HEY ! SEEMS WE HAVE A PROBLEM HERE ! ' . $name . ' IS WEIRD !';
exit;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
$arcanawayP4 = '//a[@title="' . $arcanaP4 . ' Arcana"]';
$level = $table->filterXPath($arcanawayP4 . '/parent::td/following-sibling::td')->text();
$stats = ($table->filterXPath('//td[@width="17px"]')->each(function ($node) {
return $node->text();
})); //Stats = array
//weakway preventif pour navigation, mais weak attribué plus tard pour garder sens de lecture
$weakwayP4 = "//table/tbody/tr/td/table/tbody/tr/td/table[2]/tbody/tr[2]/td/span/parent::td/parent::tr";
$status = $table->filterXPath($weakwayP4 . '/td')->each(function ($node) {
$value = $node->text();
if ($value == '-') {
$value = "None";
}
return $value;
});
$skills = $table->filterXPath('//a[@title="List of Persona 4 Skills"]/parent::th/parent::tr/parent::tbody/tr/th/a[@title!="List of Persona 4 Skills"]')->each(function ($node) {
return $node->text();
});
if (!isset($P4GO)) {
$P4GO = FALSE;
}
$persona[] =
[
"name" => $personaname,
"arcana" => $arcanaP4,
"level" => $level,
"stats" => $stats,
"status" => $status,
"skills" => $skills,
"P4G-only" => $P4GO
];
if ($name == "Izanagi-no-Okami") {
var_dump($persona);
return $persona;
}
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Persona_4_Golden"]/div[1]/div[1]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Persona_4_Golden"]/div[1]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Persona_4_Golden"]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Persona_4_Golden_2"]/div[1]/div[1]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Persona_4_Golden_2"]/div[1]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Persona_4_Golden_2"]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//span[@id="Persona_4_Golden_2"]/parent::h3/following::table[1]');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Golden"]/div[1]/div[1]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Golden"]/div[1]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//div[@data-hash="Golden"]/table');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//span[@id="Golden"]/parent::h3/following::table[1]');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
$table2 = $crawlerP4G->filterXPath('//span[@id="Persona_4_Golden"]/parent::h3/following::table[1]');
try {
$arcanaP4G = $table2->filter('[title*=" Arcana"]')->text();
} catch (InvalidArgumentException $e) {
var_dump($persona);
return $persona;
}
}
}
}
}
}
}
}
}
}
}
}
$arcanawayP4G = '//a[@title="' . $arcanaP4G . ' Arcana"]';
$level = $table2->filterXPath($arcanawayP4G . '/parent::td/following-sibling::td')->text();
$statsP4G = ($table2->filterXPath('//td[@width="17px"]')->each(function ($node) {
return $node->text();
})); //Stats = array
//weakway preventif pour navigation, mais weak attribué plus tard pour garder sens de lecture
$weakwayP4G = "//table/tbody/tr/td/table/tbody/tr/td/table[2]/tbody/tr[2]/td/span/parent::td/parent::tr";
$statusP4G = $table->filterXPath($weakwayP4G . '/td')->each(function ($node) {
$value = $node->text();
if ($value == '-') {
$value = "None";
}
return $value;
});
$skillsP4G = $table2->filterXPath('//a[@title="List of Persona 4 Skills"]/parent::th/parent::tr/parent::tbody/tr/th/a[@title!="List of Persona 4 Skills"]')->each(function ($node) {
return $node->text();
});
$persona[] =
[
"name" => $personaname,
"arcana" => $arcanaP4G,
"level" => $level,
"stats" => $statsP4G,
"status" => $statusP4G,
"skills" => $skillsP4G,
"P4G-only" => $P4GO
];
var_dump($persona);
return $persona;
}
?>