首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从nutch获取html内容

问如何从nutch获取html内容
EN

Stack Overflow用户
提问于 2011-02-26 07:16:02
回答 4查看 6.2K关注 0票数 6

有没有办法在nutch中获取每个网页的html内容,同时抓取网页?

EN

回答 4

Stack Overflow用户

发布于 2012-10-24 14:56:36

可以,您可以实际导出已爬网片段的内容。这并不简单,但对我来说效果很好。首先,使用以下代码创建一个java项目:

代码语言:javascript
复制
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;添加以下依赖项:

代码语言:javascript
复制
     <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。然后运行:

代码语言:javascript
复制
$/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/是要从中提取内容的已爬网目录(它包含'segment‘子目录),outdir是输出目录。输出文件名是从URI生成的,但是斜杠被替换为"___“。

希望能有所帮助。

票数 8
EN

Stack Overflow用户

发布于 2012-01-25 18:44:14

试试这个:

代码语言:javascript
复制
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中的内容。

票数 1
EN

Stack Overflow用户

发布于 2015-03-25 01:36:17

这是超级基础。

代码语言:javascript
复制
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 https://nutch.apache.org/apidocs/apidocs-1.2/org/apache/nutch/protocol/Content.html#getContent()上的JavaDoc

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5123757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档