php - 从网页提取的数据/文本不会插入到 mysql 数据库中

标签 php html mysql xpath

我正在尝试插入从网页中提取的文本,但它没有插入到数据库中。我正在使用 xpath 表达式来提取数据,网页上的数据位于多个 html para 或列表项标签内。

这是代码

<?php
set_time_limit(0);
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "olx";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname, $conn);
$res1 = mysql_query("SELECT * FROM `item_url` WHERE id=10");
while($r1 = mysql_fetch_array($res1))
{
    $url = $r1['url'];

    $html = file_get_contents($url);       
    $doc = new DOMDocument(); 
    @$doc->loadHTML($html);     
    $xpath = new DOMXPath($doc);

    $details = $xpath->evaluate("//div[@id='description-text']/child::div");
    foreach ($details as $detail) {
        $nodes = $detail->childNodes;
        foreach ($nodes as $node) {
        $string = $node->nodeValue;
        $string = preg_replace('/[^a-zA-Z0-9@.\-]/', ' ', $string); //allow required character
        $string = strip_tags($string); //remove html tags 
        echo $string . '<br>';
        }
    }

  mysql_query("INSERT INTO `test` (`detail`) VALUES ('$string')") or die(mysql_error());
 }
 ?>

它以这种方式显示数据

Performs skilled technical work in the maintenance, repair, replacement, and installation of air conditioning systems.

Installs, troubleshoots and repairs air conditioning units.

Replaces expansion valves, compressors, motors, coil units and other component parts.

Technicians work in residential homes, schools, hospitals, office buildings, or factories.

无法将此数据插入数据库。这是xpath节点的问题吗。每一行都在网页上的

标记内。

下面是网页的html

<div id="description-text">
    <h2 class="title-desc">
    <span>Ad details</span>
    </h2>
    <ul class="item-optionals">
    <li style="background-color: rgb(251, 251, 251);">
    </ul>
  <div style="padding-right: 30px; width: 388px;">
      <p> Performs skilled technical work in the maintenance, repair, replacement, and installation of air conditioning systems.</p>
      <p>Installs, troubleshoots and repairs air conditioning units.</p>

      <p>Replaces expansion valves, compressors, motors, coil units and other component parts.</p>

      <p>Technicians work in residential homes, schools, hospitals, office buildings, or factories.</p>

   </div>
</div>

最佳答案

你的代码很好,唯一的问题是你在处理html页面的单个节点的循环末尾调用了mysql_query,要解决这个问题,只需调用mysql_query即可最内部的 foreach 循环。

<?php
set_time_limit(0);
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "olx";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname, $conn);
$res1 = mysql_query("SELECT * FROM `item_url` WHERE id=10");
while($r1 = mysql_fetch_array($res1))
{
    $url = $r1['url'];

    $html = file_get_contents($url);       
    $doc = new DOMDocument(); 
    @$doc->loadHTML($html);     
    $xpath = new DOMXPath($doc);

    $details = $xpath->evaluate("//div[@id='description-text']/child::div");
    foreach ($details as $detail) {
        $nodes = $detail->childNodes;
        foreach ($nodes as $node) {
        $string = $node->nodeValue;
        $string = preg_replace('/[^a-zA-Z0-9@.\-]/', ' ', $string); //allow required character
        $string = strip_tags($string); //remove html tags 
        echo $string . '<br>';
        mysql_query("INSERT INTO 'test' ('detail') VALUES ('$string')") or die(mysql_error());
        }
    }


}
?>

关于php - 从网页提取的数据/文本不会插入到 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23988863/

相关文章:

html - 悬停图像不显示

php - 使用ajax功能时重新加载页面

javascript - 在 facebox 中打开表单操作...(php href 和表单操作)

html - 在 Angular js 中,如何在多选中使选定的选项加粗

html - 比较 ng-if Angular 内的日期

mysql - mariadb 10 uncompress() 返回 blob 而不是字符串

mysql - CakePHP saveAll 更新或插入相关数据

php - __get() 示例来自 Zandstra

javascript - 如何调用两个不同的 JavaScript Hover?

Android 中的 PHP 编码