6

Web ページをクロールしているときに、各 Web ページの HTML コンテンツを取得する方法はありますか?

4

4 に答える 4

8

はい、クロールされたセグメントのコンテンツを実際にエクスポートできます。簡単ではありませんが、私にとってはうまくいきます。まず、次のコードを使用して Java プロジェクトを作成します。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.nutch.protocol.Content;
import org.apache.nutch.util.NutchConfiguration;

import java.io.File;
import java.io.FileOutputStream;

public class NutchSegmentOutputParser {

public static void main(String[] args) {

    if (args.length != 2) {
        System.out.println("usage: segmentdir (-local | -dfs <namenode:port>) outputdir");
        return;
    }

    try {
        Configuration conf = NutchConfiguration.create();
        FileSystem fs = FileSystem.get(conf);


        String segment = args[0];

        File outDir = new File(args[1]);
        if (!outDir.exists()) {
            if (outDir.mkdir()) {
                System.out.println("Creating output dir " + outDir.getAbsolutePath());
            }
        }

        Path file = new Path(segment, Content.DIR_NAME + "/part-00000/data");
        SequenceFile.Reader reader = new SequenceFile.Reader(fs, file, conf);


        Text key = new Text();
        Content content = new Content();

        while (reader.next(key, content)) {
            String filename = key.toString().replaceFirst("http://", "").replaceAll("/", "___").trim();

            File f = new File(outDir.getCanonicalPath() + "/" + filename);
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(content.getContent());
            fos.close();
            System.out.println(f.getAbsolutePath());
        }
        reader.close();
        fs.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

Maven の使用をお勧めします。次の依存関係を追加します。

     <dependency>
      <groupId>org.apache.nutch</groupId>
        <artifactId>nutch</artifactId>
        <version>1.5.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>0.23.1</version>
    </dependency>

jar パッケージ (つまり、NutchSegmentOutputParser.jar) を作成します。

マシンに Hadoop をインストールする必要があります。次に実行します。

$/hadoop-dir/bin/hadoop --config \
NutchSegmentOutputParser.jar:~/.m2/repository/org/apache/nutch/nutch/1.5.1/nutch-1.5.1.jar \
NutchSegmentOutputParser nutch-crawled-dir/2012xxxxxxxxx/ outdir

ここで、nutch-crawled-dir/2012xxxxxxxxx/ は、コンテンツを抽出するクロールされたディレクトリ (「セグメント」サブディレクトリを含む) であり、outdir は出力ディレクトリです。出力ファイル名は URI から生成されますが、スラッシュは「_」に置き換えられます。

それが役に立てば幸い。

于 2012-10-24T06:56:36.280 に答える
1

これを試して:

public ParseResult filter(Content content, ParseResult parseResult, HTMLMetaTags
 metaTags, DocumentFragment doc) 
{
 Parse parse = parseResult.get(content.getUrl());
 LOG.info("parse.getText: " +parse.getText());
 return parseResult;
}

次に、 で内容を確認しhadoop.logます。

于 2012-01-25T10:44:14.447 に答える
0

その超基本。

public ParseResult getParse(Content content) {
   LOG.info("getContent: " + new String(content.getContent()));

Content オブジェクトには getContent() メソッドがあり、バイト配列を返します。Java に BA を使用して新しい String() を作成させるだけで、取得したナッチの生の html を取得できます。

Nutch 1.9 を使用しています

org.apache.nutch.protocol.Content の JavaDoc は次のとおりです https://nutch.apache.org/apidocs/apidocs-1.2/org/apache/nutch/protocol/Content.html#getContent()

于 2015-03-24T17:36:16.590 に答える
-3

はい、方法があります。cache.jsp を見て、キャッシュされたデータがどのように表示されるかを確認してください。

于 2011-03-08T17:19:09.890 に答える