2012-04-24 5 views
0

最初からメモするだけで、コンテンツはコピーされず、プロジェクトの目的でテキストを取得するプロセスを自動化したいと思います。ウェブサイト上の複数のページにあるDIVからテキストを抽出し、.txtに出力しますか?

シンプルにデザインされたウェブサイトの各ページに、特定の繰り返しDIV()のテキストを抽出したい場合は、自分の「クラス」に属しているので、簡単に

サイト内に1つのアーカイブページがあり、私が望むコンテンツを含むすべてのページのリストがあります。

サイトは

www.zenhabits.netである私は、このスクリプトのいくつかの並べ替えを達成することができた想像しますが、どこから始めれば見当がつかない。

何か助けていただきありがとうございます。

-Nathan。

+0

あなたのために書きますか?私はどこで法案を送っていますか? http://mattgemmell.com/2008/12/08/what-have-you-tried/ –

答えて

0

これはかなり簡単です。

まず、このサイトからのリンクをすべて取得し、配列にそれらすべてを投げる:

set_time_limit(0);//this could take a while... 

ignore_user_abort(true);//in case browser times out 


$html_output=file_get_contents("http://zenhabits.net/archives/"); 

# -- Do a preg_match on the html, and grab all links: 
if(preg_match_all('/<a href=\"http:\/\/zenhabits.net\/(.*)\">/',$html_output,$matches)) { 
# -- Append Data To Array 
foreach($matches[1] as $secLink) { 
    $links[] = "http://zenhabits.net/".$secLink; 
} 
    } 

私はあなたのためにこれをテストし、そして:すべて完了です

//first 3 are returning something weird, but you don't need them - so I shall remove them xD 
unset($links[0]); 
unset($links[1]); 
unset($links[2]); 

いいえ、時間をすべてのリンク(配列$リンク内)を通過し、その内容を取得する:

foreach($links as $contLink){ 

$html_output_c=file_get_contents("$contLink"); 


    if(preg_match('|<div class=\"post\">(.*)</div>|s',$html_output_c,$c_matches)) { 
    # -- Append Data To Array 
echo"data found <br>"; 
    $contentFromPage[] = $c_matches[1]; 
    } 
else{echo "no content found in: $contLink -- <br><br><br>";} 
}//end of foreach 

私は基本的にはwritte (ここでは、テキストファイルにそれを置くものとする)ループコンテンツ配列を、

そして今..あなたのために全体のクローラスクリプトナ、とあなたがそれをやりたい:

あなたはまた、使用することができます
//$contentFromPage now contains all of div class="post" content (in an array) - so do what you want with it 

    foreach($contentFromPage as $content){ 

    # -- We need a name for each text file -- 
$textName=rand()."_content_".rand().".txt";//we'll just use some numbers and text 

//define file path (where you want the txt file to be saved) 
$path="../";//we'll just put it in a folder above the script 
$full_path=$path.$textName; 

// now save the file.. 

file_put_contents($full_path,$content); 

//and that's it 

    }//end of foreach 
+0

これは素晴らしいヘルプです。私は十分にあなたに感謝することはできません。 –

0

コンテンツを抽出するSimpleHTML DOM Parserスクリプト。これは私が1.6年間使っていた非常に便利なスクリプトです。スクリプトはhttp://simplehtmldom.sourceforge.net/からダウンロードできます。これは、例とともによく説明されています。これがあなたの問題を解決するのに役立つことを願っています。

関連する問題