【问题标题】:How to get the html content from nutch如何从 nutch 获取 html 内容
【发布时间】:2011-02-25 23:16:02
【问题描述】:

有什么方法可以在抓取网页的同时获取每个网页的html内容吗?

【问题讨论】:

    标签: nutch


    【解决方案1】:

    是的,您可以准确地导出已爬网段的内容。这并不简单,但对我来说效果很好。首先,创建一个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 生成的,但是斜杠被替换为“_”。

    希望对你有帮助。

    【讨论】:

    • 很好的答案。我已经为 Spring Batch 创建了 SequenceFileReader,
    【解决方案2】:

    试试这个:

    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中的内容。

    【讨论】:

      【解决方案3】:

      它的超级基础。

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

      Content 对象有一个方法 getContent(),它返回一个字节数组。只需让 Java 使用 BA 创建一个新的 String(),您就可以获得 nutch 获取的任何原始 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()

      【讨论】:

        【解决方案4】:

        是的,有办法。查看 cache.jsp 以了解它如何显示缓存的数据。

        【讨论】:

          猜你喜欢
          • 2014-08-21
          • 2016-06-21
          • 1970-01-01
          • 2012-01-02
          • 1970-01-01
          • 2014-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多