Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3006 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  [PHP] [Curl] Problème avec les espaces

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[PHP] [Curl] Problème avec les espaces

n°2249160
tomware
Moteur à eau
Posté le 23-01-2015 à 15:00:46  profilanswer
 

Bonjour,
 
Je dois récupérer en local des milliers de pages écrites à la main en html.
 
Pour cela j'ai utilisé un script très bien construit :
 
http://www.jacobward.co.uk/working [...] ta-part-2/
 
Mon problème c'est que dès que je veux récupérer du contenu contenant des espaces (parfois variables pour le même champ), mon script ne détecte pas le contenu.
 
Je vais donc copier/coller le script que j'utilise actuellement. Et ensuite vous montrer ce qui marche et ce qui ne marche pas en esperant avoir votre aide :)

Code :
  1. <?php 
  2.     // Defining the basic cURL function
  3.     function curl($url) {
  4.         // Assigning cURL options to an array
  5.         $options = Array(
  6.             CURLOPT_RETURNTRANSFER => true// Setting cURL's option to return the webpage data
  7.             CURLOPT_FOLLOWLOCATION => TRUE// Setting cURL to follow 'location' HTTP headers
  8.             CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers
  9.             CURLOPT_CONNECTTIMEOUT => 120,   // Setting the amount of time (in seconds) before the request times out
  10.             CURLOPT_TIMEOUT => 120,  // Setting the maximum amount of time for cURL to execute queries
  11.             CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow
  12.             CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  // Setting the useragent
  13.             CURLOPT_URL => $url, // Setting cURL's URL option with the $url variable passed into the function
  14.         );
  15.        
  16.         $ch = curl_init();  // Initialising cURL
  17.         curl_setopt_array($ch, $options);   // Setting cURL's options using the previously assigned array data in $options
  18.         $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
  19.         curl_close($ch);    // Closing cURL
  20.         return $data;   // Returning the data from the function
  21.     }
  22. ?>


 

Code :
  1. <?php
  2.     // Defining the basic scraping function
  3.     function scrape_between($data, $start, $end){
  4.         $data = stristr($data, $start); // Stripping all data from before $start
  5.         $data = substr($data, strlen($start));  // Stripping $start
  6.         $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
  7.         $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
  8.         return $data;   // Returning the scraped data from the function
  9.     }
  10. ?>


Code :
  1. <?php
  2.     $scraped_page = curl("http://localhost/bibliotheque/fiches/afaire/$numero/index.html" ); // $numero est déterminé ailleurs ;)
  3. $auteur = scrape_between($scraped_page, "<div class=\"auteur\">", "</div>" );  // fonctionne parfaitement
  4. $categorie = scrape_between($scraped_page, "Catégorie :</th>
  5.                     <td class=\"table\">", "</td>" );
  6. echo"auteur : $auteur<br>"; // pour vérifier
  7. echo"catégorie : $catégorie<br>";  // pour vérifier
  8. ?>


 
 
 
Donc, suite à ce script, chaque fois qu'un contenu est défini par des balises sans espaces et bien encadrés(auteur), le contenu est parfaitement récupéré.
 
Par contre, si le contenu se retrouve entre des espaces, tab, ou autre indentation, je n'arrive pas a le récupérer. Ça affiche un blanc.
 
Voici une partie du code html de la page où je veux extraire les infos :
 

Code :
  1. <div class="auteur">Rimbaud</div> // Récupéré parfaitement !
  2. [...]
  3. <tr class="i-table"><th class="table">Catégorie :</th>                    <td class="table">Poésie</td></tr>
  4. [...]


 Et bien la ça ne marche pas pour récupérer "poésie"...
 
en utilisant donc ceci :
 

Code :
  1. $categorie = scrape_between($scraped_page, "Catégorie :</th>
  2.                     <td class=\"table\">", "</td>" );


 
 
 
 
Merci d'avance pour votre aide


Message édité par tomware le 23-01-2015 à 15:01:02
mood
Publicité
Posté le 23-01-2015 à 15:00:46  profilanswer
 

n°2249297
scvo0ne
Posté le 25-01-2015 à 18:39:23  profilanswer
 

Avec ta fonction scrape_between tu es sur la mauvaise voie.
 
Tu devrais regarder du côté de Xpath  
 
http://www.seoblackout.com/2010/01 [...] -et-xpath/
http://php.net/manual/fr/domxpath.query.php
 
En reprenant ton exemple, pour récuperer le contenu de ' <td class="table">...</td> '  l 'expression Xpath est  //td[@class='table']
 
 
Et pour gagner du temps il existe des extensions pour Chrome et Firefox qui permettent de faire du Xpath sur des pages web

n°2250338
tomware
Moteur à eau
Posté le 08-02-2015 à 13:21:38  profilanswer
 

Merci scvo0ne ca semble mieux !
 
:hello: :jap:


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  [PHP] [Curl] Problème avec les espaces

 

Sujets relatifs
Problème police d'écriture PC/téléphoneJeu de plateforme AS3 (problème de passage de niveau)
Ignorer espaces dans une recherche VBAProblème du voisin le plus proche sur R
Problème d'algo pour le jour suivant d'une date[POWER SHELL] Problème d'import de module Active Directory
Problème sur l'entame de ce code probleme producteur consommateur
Problème d'envoi de mail avec formulaire Ajax +JsonProblème avec tuple sur Python 3.4.1
Plus de sujets relatifs à : [PHP] [Curl] Problème avec les espaces


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR