0

cURL と DOM PHP を使用して Web をクロールしています。Web には、すべての製品を表示するページごとに移動できる製品セクションがあり、各ページに 9 つの製品がリストされている、より簡潔な検索用のサブセクションもあります。

商品が属するサブセクションの情報を保存する必要があります。すべてのサブセクションの URL から始めます。上記のプログラムは、サブセクションの次の 9 つの製品ページを取得する方法を示しています。

問題は、ネットワークにポストトレースがないため、Cookie にあると思われる情報を使用して Web がリダイレクトを行うことです。

例: ALL PRODUCTS セクションでは、2 番目のページの URL は次のようになります。

www.example.com/product/?n=2

サブセクションの最初のページには、次のような一意の URL があります。

www.example.com/product/subsection

問題は、次のサブセクション ページ (次の 9 製品) へのリンクが

www.example.com/product/?n=2

URL はすべての製品セクションと同じですが、サブセクションの製品が表示されます。問題は、SUBSECTION ページではなく ALL PRODUCTS ページが表示されることです。

Cookie を試してみましたが、明確な結果が得られません。なにか提案を?

<?php
    private ckfile;

    public function main()
    {
        $this->ckfile = tempnam ("C:/Web/", "CURLCOOKIE");
        $copy = $this->get_page();

        $next_visit = $this->link_next($copy);
        while($next_visit != false){//it's not last page
            $copy = $this->get_page($next_visit,$get_name($next_visit));
            $next_visit = $this->link_next($copy);
        }
    }

    public function get_page($URL = "http://www.example.com" , $nombre = "example" )
    {       
        $ch = curl_init();
        $options = array(
                        CURLOPT_HTTPHEADER      => array("Accept-Language: es-es,en"),
                        CURLOPT_USERAGENT       => "Googlebot/2.1 (+http://www.google.com/bot.html)",
                        CURLOPT_AUTOREFERER     => true,         // set referer on redirect ,
                        CURLOPT_ENCODING        => "",               //allow all encodings
                        CURLOPT_FOLLOWLOCATION  => true,         // follow redirects
                        CURLOPT_HEADER          => false,               
                        CURLOPT_CONNECTTIMEOUT  => 120,          // timeout on connect 
                        CURLOPT_TIMEOUT         => 120,          // timeout on response 
                        CURLOPT_MAXREDIRS       => 10,           // stop after 10 redirects 
                        CURLOPT_COOKIEFILE      => $this->ckfile,
                        CURLOPT_COOKIEJAR       =>  $this->ckfile,
                        CURLOPT_RETURNTRANSFER  => true,
                        CURLOPT_URL             => $URL
                        );
        curl_setopt_array($ch, $options);

        $g = 'C:/Web/'.$nombre.'.html';
        if(!is_file($g)){
            $fp=fopen ($g, "w");
            curl_setopt ($ch,CURLOPT_FILE, $fp);
            $trash = curl_exec ($ch); // don't browse them       
            fclose($fp); 
        }
        curl_close ($ch);
    return $g;      
    }

    public function link_next($value)
    {
        # function that searches the DOM for a link and returns a well formed URL
            # or returns false if doesn't find one( last page)
    }
?>
4

1 に答える 1

2

複数の呼び出しを行うには、curl multi を使用します。

$ch = curl_multi_init();

いいえ

    $ch = curl_init();

例については、この投稿を参照してください同じページへの複数の PHP cUrl 投稿

于 2012-11-15T15:24:15.923 に答える