Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java web爬虫中的HTML解析_Java_Html_Html Parsing_Web Crawler - Fatal编程技术网

Java web爬虫中的HTML解析

Java web爬虫中的HTML解析,java,html,html-parsing,web-crawler,Java,Html,Html Parsing,Web Crawler,除了我前面的问题:,我尝试使用以下方法从HTML标记(在本例中为title)中提取信息: public static void parsePage() throws IOException, BadLocationException { HTMLEditorKit kit = new HTMLEditorKit(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); doc.putPropert

除了我前面的问题:,我尝试使用以下方法从HTML标记(在本例中为title)中提取信息:

public static void parsePage() throws IOException, BadLocationException 
{
    HTMLEditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
    Reader HTMLReader = new InputStreamReader(testURL.openConnection()
            .getInputStream());
    kit.read(HTMLReader, doc, 0);

    // Create an iterator for all HTML tags.
    ElementIterator it = new ElementIterator(doc);
    Element elem;

    while ((elem = it.next()) != null) 
    {
        if (elem.getName().equals("title")) 
        {
            System.out.println("found title tag");
        }
    }
}
这是有效的,因为它告诉我它找到了标签。 我正在努力解决的是如何提取它们后面/里面包含的信息

我在网站上发现了这个问题:,但是它声明它只适用于格式良好的HTML。 我希望还有别的办法

任何提示都值得欣赏。

尝试使用

试用


结果表明,将方法更改为此会产生所需的结果:

    {
            HTMLEditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
            Reader HTMLReader = new InputStreamReader(testURL.openConnection().getInputStream());
            kit.read(HTMLReader, doc, 0);
            String title = (String) doc.getProperty(Document.TitleProperty);
            System.out.println(title);
    }

我想我对迭代器/元素的东西进行了一场白费力气的追逐。

结果表明,将方法更改为此会产生所需的结果:

    {
            HTMLEditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
            Reader HTMLReader = new InputStreamReader(testURL.openConnection().getInputStream());
            kit.read(HTMLReader, doc, 0);
            String title = (String) doc.getProperty(Document.TitleProperty);
            System.out.println(title);
    }

我想我对迭代器/元素之类的东西的追求是白费力气。

谢谢Alexey。有没有不使用外部库的方法可以做到这一点?如果你需要一个快速而肮脏的一次性解决方案,你可以使用正则表达式来提取标题,但是,一般来说,避免使用正则表达式来解析HTML,我理解使用正则表达式来解析HTML是不可取的。在这种情况下,我只需要标题信息。在这种情况下,如果您不喜欢有额外的依赖项,我将使用一个简单的regexp。谢谢Alexey。有没有不使用外部库的方法可以做到这一点?如果你需要一个快速而肮脏的一次性解决方案,你可以使用正则表达式来提取标题,但是,一般来说,避免使用正则表达式来解析HTML,我理解使用正则表达式来解析HTML是不可取的。在本例中,我只需要标题信息。在本例中,如果您不喜欢有额外的依赖项,我将使用一个简单的regexp。
    {
            HTMLEditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
            Reader HTMLReader = new InputStreamReader(testURL.openConnection().getInputStream());
            kit.read(HTMLReader, doc, 0);
            String title = (String) doc.getProperty(Document.TitleProperty);
            System.out.println(title);
    }