【问题标题】:Cut desired HTML part from DOM object从 DOM 对象中剪切所需的 HTML 部分
【发布时间】:2017-07-29 22:35:09
【问题描述】:

我正在尝试从我的 DOM 对象中获取一个特定的 css 类。我使用 simplehtmldom 库。

1) 图书馆

simplehtmldom.sourceforge.net

2)因为我的localhost因为某种原因不支持fopen,所以我使用CURL库来获取HTML,源码:

http://simplehtmldom.sourceforge.net/manual_faq.htm

3) 现在,我的脚本看起来像这样。它为我提供了我想要的网站的 HTML 源代码。

<?php
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://hokejbal.cz/1-liga/tabulky/");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    print $result;

    str_get_dom;
    $ret = $html->find('.standings tablesort tablesorter tablesorter-default');
?>

4) 现在,我只想获取网站的一部分。正是这张表:

<table class="standings tablesort tablesorter tablesorter-default">

我在谷歌浏览器站长工具中找到的

不幸的是,当我运行脚本时,我得到了整个 HTML 页面,而不仅仅是所需的部分。我做错了什么?

【问题讨论】:

    标签: html css parsing simple-html-dom


    【解决方案1】:

    选择器将是'.standings.tablesort.tablesorter.tablesorter-default'

    更新:试试下面的代码。

    <?php
        $html = file_get_html('http://hokejbal.cz/1-liga/tabulky/');  
        $ret = $html->find('table.standings', 0);
        print $ret;
    ?>
    

    【讨论】:

    • 或者只是给它一些唯一的 ID 和/或类class='table-01 ...
    • 不,它不起作用,@Miro 我不能给它唯一的类,因为我不是源代码的所有者。
    • 源中是否还有其他具有standings 类的表?我没有,试试'table.standings'
    • @MaartenvanTjonger 不,它也无济于事。也许看看网站?有两张同级的表,这不是问题吗? hokejbal.cz/1-liga/tabulky
    • 尝试使用$html-&gt;find('table.standings', 0); 获取第一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2015-07-17
    • 2018-12-24
    • 1970-01-01
    • 2022-10-14
    • 2017-05-03
    相关资源
    最近更新 更多