【问题标题】:Simple dom parser insert into database简单的 dom 解析器插入数据库
【发布时间】:2014-01-07 16:20:56
【问题描述】:

我想在我的数据库中插入一些元素,但我希望 $pavadinimas 和 %kaina 在一行中,没有不同。此外,如果我可以在网站的所有页面中生成我的元素,那将是非常酷的,但是我插入了超过 2 个链接,我在刷新我的网页时出现错误,该页面无法加载。这是我的代码。感谢帮助!

<?php // example of how to modify HTML contents


include_once('simple_html_dom.php');

// Create DOM from URL or file

$html = file_get_html('https://www.varle.lt/mobilieji-telefonai/');

foreach($html->find('span[class=inner]') as $pavadinimas) {
    $pavadinimas = str_replace("<span class=", " ", $pavadinimas);
    $pavadinimas = str_replace("inner>", " ", $pavadinimas);
    $pavadinimas = str_replace("<span>", " ", $pavadinimas);
    $pavadinimas = str_replace("</span></span>", " ", $pavadinimas);
    $pavadinimas = str_replace('"inner">   ', " ", $pavadinimas);
}

foreach($html->find('span[class=price]') as $kaina) {
    $kaina = str_replace("Lt", " ", $kaina);
    $kaina = str_replace("<span class=", " ", $kaina);
    $kaina = str_replace("price", " ", $kaina);
    $kaina = str_replace("</span>", " ", $kaina);
    $kaina = str_replace(",<sup>99</sup>", " ", $kaina);
    $kaina = str_replace(",<sup>99</sup>", " ", $kaina);
    $kaina = str_replace("               ", " ", $kaina);
    $kaina = str_replace('" ">', " ", $kaina);
    $kaina = str_replace("              ", " ", $kaina);
    $query = "insert into telefonai (pavadinimas,kaina) VALUES (?,?)";
    $this->db->query($query, array($pavadinimas,$kaina));
}
?>

【问题讨论】:

  • 您可以在问题中使用更多标签。也许PHP por exmaple。这样,更多的人会阅读它并可能对您有所帮助。
  • 使用str_replace 是浪费时间,因为您可以使用$pavadinimas-&gt;plaintext 轻松获取标签的内容...有关详细信息,请查看Manual... [已发布答案]

标签: php database parsing simple-html-dom domparser


【解决方案1】:

一步一步来……

首先从一个页面(例如第一页)获取所有想要的信息......这个想法是:

  • 获取所有电话块:$phones = $html-&gt;find('a[data-id]');
  • 在循环中,从每个区块中获取想要的信息(名称、价格)
  • 在 db 中插入这些信息(我无法为 db 提供帮助,因为我有一段时间没有使用过,但您可以自己完成,并不难)

现在您的代码适用于一个页面,让我们试着让它适用于所有页面,因为:

  • 所有页面都具有相同的结构,因此我们可以使用上述相同的方法/代码提取数据
  • 要抓取的下一页的链接包含在Next 按钮中,因此当找不到此链接时我们将停止

这里有一段代码总结了我们上面所说的所有内容:

$url = "https://www.varle.lt/mobilieji-telefonai/";

// Start from the main page
$nextLink = $url;

// Loop on each next Link as long as it exsists
while ($nextLink) {
    echo "<hr>nextLink: $nextLink<br>";
    //Create a DOM object
    $html = new simple_html_dom();
    // Load HTML from a url
    $html->load_file($nextLink);

    /////////////////////////////////////////////////////////////
    /// Get phone blocks and extract info (also insert to db) ///
    /////////////////////////////////////////////////////////////
    $phones = $html->find('a[data-id]');

    foreach($phones as $phone) {
        // Get the link
        $linkas = $phone->href;

        // Get the name
        $pavadinimas = $phone->find('span[class=inner]', 0)->plaintext;

        // Get the name price and extract the useful part using regex
        $kaina = $phone->find('span[class=price]', 0)->plaintext;
        // This captures the integer part of decimal numbers: In "123,45" will capture "123"... Use @([\d,]+),?@ to capture the decimal part too
        preg_match('@(\d+),?@', $kaina, $matches);
        $kaina = $matches[1];

        echo $pavadinimas, " #----# ", $kaina, " #----# ", $linkas, "<br>";

        // INSERT INTO DB HERE
        // CODE
        // ...
    }
    /////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////

    // Extract the next link, if not found return NULL
    $nextLink = ( ($temp = $html->find('div.pagination a[class="next"]', 0)) ? "https://www.varle.lt".$temp->href : NULL );

    // Clear DOM object
    $html->clear();
    unset($html);
}

输出

nextLink: https://www.varle.lt/mobilieji-telefonai/
Samsung Phone I9300 Galaxy SIII Juodas #----# 1099 #----# https://www.varle.lt/mobilieji-telefonai/samsung-phone-i9300-galaxy-siii-juodas.html
Samsung Galaxy S2 Plus I9105 Pilkai mėlynas #----# 739 #----# https://www.varle.lt/mobilieji-telefonai/samsung-galaxy-s2-plus-i9105-pilkai-melynas.html
Samsung Phone S7562 Galaxy S Duos baltas #----# 555 #----# https://www.varle.lt/mobilieji-telefonai/samsung-phone-s7562-galaxy-s-duos-baltas--457135.html
...

nextLink: https://www.varle.lt/mobilieji-telefonai/?p=2
LG T375 Mobile Phone Black #----# 218 #----# https://www.varle.lt/mobilieji-telefonai/lg-t375-mobile-phone-black.html
Samsung S6802 Galaxy Ace Duos black #----# 579 #----# https://www.varle.lt/mobilieji-telefonai/samsung-s6802-galaxy-ace-duos-black.html
Mobilus telefonas Samsung Galaxy Ace Onyx Black | S5830 #----# 559 #----# https://www.varle.lt/mobilieji-telefonai/mobilus-telefonas-samsung-galaxy-ace-onyx-black.html
...

...
...

Working DEMO

注意代码可能需要一段时间来解析所有页面,因此 php 可能会返回此错误Fatal error: Maximum execution time of 30 seconds exceeded ...。然后,只需像这样延长最大执行时间:

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

【讨论】:

  • 非常感谢,您的回答安全了我一千个小时的搜索时间。
  • @user3125624,不客气。如果这解决了您的问题,请将答案标记为已接受...
  • varle.lt/mobilieji-telefonai/lg-l3-ii-e430-black.html" 我想得到手机的链接,但是网站上有很多href,我试试$linkas=$html->find("a[href* ='varle']");等等,但没有什么好事发生
  • 请仔细检查网页的源代码......如果你这样做,你会看到我们在开始时抓取的锚(块)包含你正在寻找的链接...... . 所以你可以简单地使用$phone-&gt;href; retirieve url ... [代码更新]
  • 我需要解析其他网店的数据。他们没有这样的电话结构,我是按照你的例子来处理的,但没有什么好的。$url = "pigu.lt/foto_gsm_mp3/mobilieji_telefonai";我正在尝试 $phones = $html->find('a[alt]');但解析器没有找到
猜你喜欢
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
相关资源
最近更新 更多