如何在PHP中使用simple_html_dom导入多个URL?

时间:2018-06-22 09:14:15

标签: php

概念是,我必须导入包含链接的excel。当我们导入excel时,然后将多个链接插入数据库。如何使用file_get_html给出多个URL。 我的代码在这里,

<?php if(!empty($_FILES["excel_file"]))  {  
  $connect = mysqli_connect("localhost", "root", "", "mydb");  
  $file_array = explode(".", $_FILES["excel_file"]["name"]);  
  if($file_array[1] == "xlsx")  
  {  
       include("PHPExcel/IOFactory.php");  
       $output = '';  
       $output .= "  
       <label class='text-success'>Data Inserted</label>  
            <table class='table table-bordered'>  
                 <tr>  
                      <th>link</th>  

                 </tr>  
                 ";  
       $object = PHPExcel_IOFactory::load($_FILES["excel_file"]["tmp_name"]);  
       foreach($object->getWorksheetIterator() as $worksheet)  
       {  
            $highestRow = $worksheet->getHighestRow();  
            for($row=2; $row<=$highestRow; $row++)  
            {  

         $link = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());

         include('simple_html_dom.php'); $html = file_get_html("$link");


foreach($html->find('table#job-content') as $article) {

$item['jobtitle']    = $article->find('b.jobtitle',0)->plaintext;

$item['companyname']    = $article->find('span.company',0)->plaintext;

$item['city']    = $article->find('span.location',0)->plaintext;

$item['job_type']    = $article->find('span.no-wrap',0)->plaintext;

$item['description'] = $article->find('td.snip',0)->plaintext;

$articles[] = $item;} $query = "INSERT INTO jobpost (jobtitle, companyname, city, job_type, description, status, employeeemail, link) VALUES ('". mysqli_real_escape_string($connect, $item['jobtitle'])  ."', '". mysqli_real_escape_string($connect, $item['companyname'])  ."', '". mysqli_real_escape_string($connect, $item['city'])  ."', 'Full-time', '". mysqli_real_escape_string($connect, $item['description'])  ."', '', 'admin@inuson.com', '$link')";



                 mysqli_query($connect, $query);  
                 $output .= '  <tr>  <td>'.$link.'</td>  </tr>  ';  } $output .= '</table>';  echo $output;  }  else  {  echo '<label class="textdanger">Invalid File</label>';  }  }?>  

请帮助我,谢谢。

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码。

<?php if(!empty($_FILES["excel_file"]))  {
  $connect = mysqli_connect("localhost", "root", "", "mydb");
  $file_array = explode(".", $_FILES["excel_file"]["name"]);
  if($file_array[1] == "xlsx")
  {
   include("PHPExcel/IOFactory.php");
   $output = '';
   $output .= "
   <label class='text-success'>Data Inserted</label>
   <table class='table table-bordered'>
   <tr>
   <th>link</th>

   </tr>
   ";
   $object = PHPExcel_IOFactory::load($_FILES["excel_file"]["tmp_name"]);
   foreach($object->getWorksheetIterator() as $worksheet){
    $highestRow = $worksheet->getHighestRow();
    for($row=2; $row<=$highestRow; $row++){

     $link = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());

     include('simple_html_dom.php');
     $html = file_get_html("$link");

     foreach($html->find('a') as $article){
       $get_link .= $article->href.", ";
     }


     foreach($html->find('table#job-content') as $article) {

      $item['jobtitle']    = $article->find('b.jobtitle',0)->plaintext;

      $item['companyname']    = $article->find('span.company',0)->plaintext;

      $item['city']    = $article->find('span.location',0)->plaintext;

      $item['job_type']    = $article->find('span.no-wrap',0)->plaintext;

      $item['description'] = $article->find('td.snip',0)->plaintext;

    }
    $query = "INSERT INTO jobpost (jobtitle, companyname, city, job_type, description, status, employeeemail, link) VALUES ('". mysqli_real_escape_string($connect, $item['jobtitle'])  ."', '". mysqli_real_escape_string($connect, $item['companyname'])  ."', '". mysqli_real_escape_string($connect, $item['city'])  ."', 'Full-time', '". mysqli_real_escape_string($connect, $item['description'])  ."', '', 'admin@inuson.com', '$get_link')";

    $get_link = NULL;

    mysqli_query($connect, $query);
    $output .= '  <tr>  <td>'.$link.'</td>  </tr>  ';
  }
  $output .= '</table>';  echo $output;
}  else  {
  echo '<label class="textdanger">Invalid File</label>';
}
}?>

答案 1 :(得分:0)

我得到了答案。

<?php  if(!empty($_FILES["excel_file"])) {  
  $connect = mysqli_connect("localhost", "root", "", "mydb");  
  $file_array = explode(".", $_FILES["excel_file"]["name"]);  
  if($file_array[1] == "xlsx")  
  {  
       include("PHPExcel/IOFactory.php");  
       $output = '';  
       $output .= "  
       <label class='text-success'>Data Inserted</label>  
            <table class='table table-bordered'>  
                 <tr>  
                      <th>link</th>  

                 </tr>  
                 ";  
       $object = PHPExcel_IOFactory::load($_FILES["excel_file"]["tmp_name"]);  
       foreach($object->getWorksheetIterator() as $worksheet)  
       {  
            $highestRow = $worksheet->getHighestRow();  
            for($row=2; $row<=$highestRow; $row++)  
            {  



                 $link = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue()); require_once( 'simple_html_dom.php' );$url  = array( "$link" );$html = array();foreach( $url as $key=>$value ) { 

   // get html plain-text for webpage & assign to html array.
      $html[ $key ] = file_get_html( $value ); 





    foreach($html[ $key ]->find('table#job-content') as $article) {

  $item['jobtitle']    = $article->find('b.jobtitle',0)->plaintext;

  $item['companyname']    = $article->find('span.company',0)->plaintext;

  $item['city']    = $article->find('span.location',0)->plaintext;

  $item['job_type']    = $article->find('span.no-wrap',0)->plaintext;

  $item['description'] = $article->find('td.snip',0)->plaintext;

 $articles[] = $item;
}     } echo "<br />" ;$servername = "localhost"; $username = "root"; $password = "";$dbname = "mydb"; $conn = new mysqli($servername, $username, $password, $dbname);if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);} if(mysqli_real_escape_string($conn, $item['job_type'] == '')){ $sql = "INSERT INTO jobpost (jobtitle, companyname, city, job_type, description, status, employeeemail, link) VALUES ('". mysqli_real_escape_string($conn, $item['jobtitle'])  ."', '". mysqli_real_escape_string($conn, $item['companyname'])  ."', '". mysqli_real_escape_string($conn, $item['city'])  ."', 'Full-time', '". mysqli_real_escape_string($conn, $item['description'])  ."', '', 'admin@inuson.com', '$link')"; } else{ $sql = "INSERT INTO jobpost (jobtitle, companyname, city, job_type, description, status, employeeemail, link) VALUES ('". mysqli_real_escape_string($conn, $item['jobtitle'])  ."', '". mysqli_real_escape_string($conn, $item['companyname'])  ."', '". mysqli_real_escape_string($conn, $item['city'])  ."', '". mysqli_real_escape_string($conn, $item['job_type'])  ."', '". mysqli_real_escape_string($conn, $item['description'])  ."', '', 'admin@inuson.com', '$link')";} if ($conn->query($sql) === TRUE) {
echo "New link successfully inserted"; } else {
echo "Error: " . $sql . "<br>" . $conn->error;} $conn->close();

                 $output .= '  
                 <tr>  
                      <td>'.$link.'</td>  

                 </tr>  
                 ';              

            }  
       }  
       $output .= '</table>';  
       echo $output;  
  }  
  else  
  {  
       echo '<label class="text-danger">Invalid File</label>';  
  }  }   ?>