Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/09/2014, 13:43
tdcp
 
Fecha de Ingreso: febrero-2014
Ubicación: Madrid
Mensajes: 19
Antigüedad: 10 años, 2 meses
Puntos: 0
Extraer datos de una web

Hola, tengo un problema al extraer datos de una web y guardarlos en una BD.
Solo me guarda el último registro.
Dejo el código que estoy utilizando por si me podeis ayudar en lo que estoy fallando.
Gracias

Código PHP:
Ver original
  1. <?php
  2. require("phpsqlsearch_dbinfo.php");
  3. $connection=mysql_connect (localhost, $username, $password);
  4. if (!$connection) {
  5.   die("Not connected : " . mysql_error());
  6. }
  7.  
  8. // Set the active mySQL database
  9. $db_selected = mysql_select_db($database, $connection);
  10. mysql_query("SET NAMES utf8");
  11. if (!$db_selected) {
  12.   die ("Can\'t use db : " . mysql_error());
  13. }
  14.  
  15. $extrae = $_POST['url'];
  16. $type = $_POST['type'];
  17. $provincias = $_POST['county'];
  18. $paises = $_POST['country'];
  19. // Use internal libxml errors -- turn on in production, off for debugging
  20. // Createa a new DomDocument object
  21. $dom = new DomDocument;
  22. // Load the HTML
  23. $dom->loadHTMLFile($extrae);
  24. // Create a new XPath object
  25. $xpath = new DomXPath($dom);
  26. //Query all <td> nodes containing specified class name
  27. $titulos = $xpath->query('//a[@class="url"]');
  28. $telefonos = $xpath->query('//p[@class="tel"]');
  29. $direcciones = $xpath->query('//span[@class="street-address"]');
  30. $ciudades = $xpath->query('//a[@class="locality"]');
  31. $lats = $xpath->query('//span[@class="latitude hidden"]');
  32. $lngs = $xpath->query('//span[@class="longitude hidden"]');
  33. // Set HTTP response header to text/html for debugging output
  34. header('Content-type: text/html; charset=utf-8');
  35. //Traverse the DOMNodeList object to output each DomNode's nodeValue
  36. foreach ($titulos as $titulo) {
  37.     $name = ltrim($titulo->nodeValue);
  38.     //echo $name."<br>";
  39. }
  40. foreach ($direcciones as $direccion) {
  41.     $address = $direccion->nodeValue;
  42.     //echo $address."<br>";
  43. }
  44. foreach ($ciudades as $ciudad) {
  45.     $city = $ciudad->nodeValue;
  46.     //echo $city." ".$provincias." ".$paises."<br>";
  47. }
  48. foreach ($telefonos as $telefono) {
  49.     $phone = $telefono->nodeValue;
  50.     //echo $phone."<br>";
  51. }
  52. foreach ($lats as $lat) {
  53.     $latitud = $lat->nodeValue;
  54.     //echo $latitud."<br>";
  55. }
  56. foreach ($lngs as $lng) {
  57.     $longitud = $lng->nodeValue;
  58.     //echo $longitud."<br>";
  59. }
  60. $sqlins = "INSERT INTO markersprueba (name, type, address, city, county, country, phone, lat, lng, fecAlta) ".
  61.   "VALUES ('$name', '$type', '$address', '$city', '$provincias', '$paises', '$phone', '$latidud', '$longitud', CURDATE())";
  62. $resultins = mysql_query($sqlins) or die("No se ha podido insertar el log".mysql_error());
  63. ?>