dongsui3297 2018-06-22 09:14
浏览 37
已采纳

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

The concept is, I have to import excel which contains links. When we import excel, then insert multiple links into the database. How to give multiple urls using file_get_html. My code is here,

<?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>';  }  }?>  

Please help me, Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dra11767 2018-06-23 06:44
    关注

    I got an answer.

    <?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>';  
      }  }   ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值