1

私は、HTML div コードを自分の Web サイト ページに取得し、Web サイトに表示するページに取り組んでいます。私のコードは次のようになります。

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をエコーし​​たいHTMLコード。

<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>
4

1 に答える 1

0

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>
于 2015-08-03T11:59:35.237 に答える