par
Marssac » 27 août 2016, 10:45
Bonjour,
J'ai une fonction d'extraction de données sur des pages web qui ne renvoie plus rien quand elle est appelée dans une boucle while .
Avec l'URL dans une variable OK
function extraction($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three);
return $unit;
}
///////////////////////////////
function url($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT,'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scrap_time = curl_exec($ch);
curl_close($ch);
$text = $curl_scrap_time;
$start0 = '<table class="infobox_v2">';
$end0 = '</table>';
$unit0 = extraction($text, $start0, $end0);
return $unit0;
}
///////////////////////////////
$page_suivante = "https://fr.wikipedia.org/wiki/Pont_Yavuz_Sultan_Selim";
echo url($page_suivante);
Par l'intermédiaire d'une boucle while KO
function extraction($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three);
return $unit;
}
///////////////////////////////
function url($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT,'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scrap_time = curl_exec($ch);
curl_close($ch);
$text = $curl_scrap_time;
$start0 = '<table class="infobox_v2">';
$end0 = '</table>';
$unit0 = extraction($text, $start0, $end0);
return $unit0;
}
//////////////////////////////////////////////
if (!$fp = fopen("liens_a_suivre","r")) {
echo "Echec de l'ouverture du fichier";
exit;
}
else {
while(!feof($fp)) {
$Ligne = fgets($fp,255);
echo url($Ligne);
}
fclose($fp);
}
Dans le fichier "liens_a_suivre" exemple :
https://fr.wikipedia.org/wiki/Dilma_Rousseff
https://fr.wikipedia.org/wiki/Attentat_de_Cizre_du_26_ao%C3%BBt_2016
https://fr.wikipedia.org/wiki/Pont_Yavuz_Sultan_Selim
Je sèche grave, merci par avance pour votre bienveillance à l'égard d'un néophyte en PHP.
Cordialement
Bonjour,
J'ai une fonction d'extraction de données sur des pages web qui ne renvoie plus rien quand elle est appelée dans une boucle while .
[b]Avec l'URL dans une variable [color=#FF0000]OK[/color][/b]
[php]function extraction($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three);
return $unit;
}
///////////////////////////////
function url($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT,'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scrap_time = curl_exec($ch);
curl_close($ch);
$text = $curl_scrap_time;
$start0 = '<table class="infobox_v2">';
$end0 = '</table>';
$unit0 = extraction($text, $start0, $end0);
return $unit0;
}
///////////////////////////////
$page_suivante = "https://fr.wikipedia.org/wiki/Pont_Yavuz_Sultan_Selim";
echo url($page_suivante);[/php]
[b]Par l'intermédiaire d'une boucle while [color=#FF0000]KO[/color][/b]
[php]function extraction($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three);
return $unit;
}
///////////////////////////////
function url($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT,'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scrap_time = curl_exec($ch);
curl_close($ch);
$text = $curl_scrap_time;
$start0 = '<table class="infobox_v2">';
$end0 = '</table>';
$unit0 = extraction($text, $start0, $end0);
return $unit0;
}
//////////////////////////////////////////////
if (!$fp = fopen("liens_a_suivre","r")) {
echo "Echec de l'ouverture du fichier";
exit;
}
else {
while(!feof($fp)) {
$Ligne = fgets($fp,255);
echo url($Ligne);
}
fclose($fp);
}[/php]
Dans le fichier "liens_a_suivre" exemple :
[html]https://fr.wikipedia.org/wiki/Dilma_Rousseff
https://fr.wikipedia.org/wiki/Attentat_de_Cizre_du_26_ao%C3%BBt_2016
https://fr.wikipedia.org/wiki/Pont_Yavuz_Sultan_Selim[/html]
Je sèche grave, merci par avance pour votre bienveillance à l'égard d'un néophyte en PHP.
Cordialement