简单的HTML DOM代码问题


Simple HTML DOM code issue

我正在一个页面上工作,得到htmldiv代码到我的网站页面,并在网站上显示。我的代码是这样的:

require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url); if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
   echo $element;
        }
     }

它不会显示任何东西,因为链接中包含了东西。我想要它显示在我的网站页面的页面的特定div。我应该怎么做/编辑在我的代码,使它显示特定的div到我的页面。

编辑:我的HTML代码,我想在它下面回显div。

<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
}
}
</div>
</div>
</div>

在您编辑之前,我没有意识到您已经在尝试将echo $element放入正确的位置。但似乎在你的情况下,你只是忘记了关闭?> php标签。

<div id="main_b">
    <div class="wrapper">
        <div id="main_content">
            <?php
                require './simpledom.php';
                $url = 'https://cit2.net/index.php?topic=75443.0';
                $html = file_get_html($url);
                if (!empty($html)) {
                    $element = $html->find('div[id=msg_783326]');
                    if (!empty($element)) {
                        echo $element;
                    }
                }
            ?>
        </div>
    </div>
</div>

编辑:仍然没有输出任何东西?让我们开始调试过程:

<div id="main_b">
    <div class="wrapper">
        <div id="main_content">
            <?php
                require './simpledom.php';
                $url = 'https://cit2.net/index.php?topic=75443.0';
                $html = file_get_html($url);
                if (!empty($html)) {
                    $element = $html->find('div[id=msg_783326]');
                    if (!empty($element)) {
                        echo $element;
                    } else {
                        $element = 'element was empty';
                    }
                } else {
                    $element = 'html was empty';
                }
                echo $element;
            ?>
        </div>
    </div>
</div>

更新:尝试以编程方式登录

<div id="main_b">
    <div class="wrapper">
        <div id="main_content">
            <?php
                require './simpledom.php';
                $login_url = 'https://cit2.net/index.php?action=login';
                $topic_url = 'https://cit2.net/index.php?topic=75443.0';
                $username = '';
                $password = '';
                $params = [
                    'user' => $username,
                    'passwrd' => $password
                ];
                $params = http_build_query($params);
                // init curl
                $ch = curl_init();
                // set the url to work with
                curl_setopt( $ch, CURLOPT_URL, $login_url );
                // enable http post
                curl_setopt( $ch, CURLOPT_POST, 1 );
                // set the post parameters
                curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
                // handle cookies for the login
                curl_setopt( $ch, CURLOPT_COOKIEJAR, 'cookie.txt' );
                //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
                //not to print out the results of its query.
                //Instead, it will return the results as a string return value
                //from curl_exec() instead of the usual true/false.
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
                // execute the request
                $store = json_decode( curl_exec( $ch ) );
                if ( $store->status == 'ok' ) {
                    $html = file_get_html($topic_url);
                    if (!empty($html)) {
                        $element = $html->find('div[id=msg_783326]');
                        if (!empty($element)) {
                            echo $element;
                        } else {
                            $element = 'element was empty';
                        }
                    } else {
                        $element = 'html was empty';
                    }
                } else {
                    $elment = 'request was bad';
                }
                echo $element;
            ?>
        </div>
    </div>
</div>