Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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 使用jsoup从博客中提取评论_Java - Fatal编程技术网

Java 使用jsoup从博客中提取评论

Java 使用jsoup从博客中提取评论,java,Java,请帮助使用jsoup从类似博客的blogger中提取评论 我可以得到标题,但如何提取人们对某个讨论主题发表的所有评论 package com.hascode.samples.jsoup; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class WebScraper { public

请帮助使用jsoup从类似博客的blogger中提取评论

我可以得到标题,但如何提取人们对某个讨论主题发表的所有评论

package com.hascode.samples.jsoup;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class WebScraper {
 public static void main(final String[] args) throws IOException {
 Document doc = Jsoup.connect("http://www.hascode.com/")
 .userAgent("Mozilla").timeout(6000).get();
 String title = doc.title(); // parsing the page's title
 System.out.println("The title of www.hascode.com is: " + title);
 Elements heading = doc.select("h2 > a"); // parsing the latest article's
 // heading
 System.out.println("The latest article is: " + heading.text());
 System.out.println("The article's URL is: " + heading.attr("href"));
 Elements editorial = doc.select("div.BlockContent-body small");
 System.out.println("The was created: " + editorial.text());
 }
}

我试图使用Jframe提取注释,但没有输出。这是我的密码:

public class SimpleWebCrawler extends JFrame {

    JTextField yourInputField = new JTextField(20);
    static JTextArea _resultArea = new JTextArea(100, 100);
    JScrollPane scrollingArea = new JScrollPane(_resultArea);
    private final static String newline = "\n";

    public SimpleWebCrawler() throws MalformedURLException {

        _resultArea.setEditable(false);
        System.out.println("Please enter the website  :");
        Scanner scan2 = new Scanner(System.in);
        String word2 = scan2.nextLine();

        try {
            URL my_url = new URL("http://" + word2 + "/");
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    my_url.openStream()));
            String strTemp = "";
            while (null != (strTemp = br.readLine())) {
                _resultArea.append(strTemp + newline);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        _resultArea.append("\n");
        _resultArea.append("\n");
        _resultArea.append("\n");


        String url = "http://" + word2 + "/";
        print("Fetching %s...", url);

        try{
        Document articlePage = Jsoup.connect(url).get();
        Elements comments = articlePage.select(".comments .comment-body");


        System.out.println("\n");

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"));
        _resultArea.append("\n");
        for (Element comment : comments) {
            print("  %s  ",  comment.text());

            bw.write(comment.text());
            bw.write(System.getProperty("line.separator"));
        }
        bw.flush();
        bw.close();
        } catch (IOException e1) {

        }
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingArea, BorderLayout.CENTER);

        this.setContentPane(content);
        this.setTitle("Crawled Links");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.pack();


        }

        private static void print(String msg, Object... args) {

            _resultArea.append(String.format(msg, args) +newline);
        }

        private static String trim(String s, int width) {
            if (s.length() > width)
                return s.substring(0, width - 1) + ".";
            else
                return s;
        }

        //.. Get the content pane, set layout, add to center


    public static void main(String[] args) throws IOException {

        JFrame win = new SimpleWebCrawler();
        win.setVisible(true);

    }
}

只需打开文章页面并从中删除评论。每个注释都是一个
  • 元素,位于
    中,带有类
    commentsList
    ,因此您可以按如下方式获取它们:

    Document articlePage = Jsoup.connect(heading.attr("href")).get();
    Elements comments = articlePage.select(".commentsList li");
    for (Element comment : comments) {
        System.out.println("Comment: " + comment.text());
    }
    

    好的,我试过了,但这是我的示例博客,我想从……
    Elements comments=articlePage中提取评论。选择(“.comments.comment body”)
    应该在这里起作用。一般来说,您需要找到一个选择器,该选择器将只选择DOM树中正确的元素。@baksteen记住向上投票有用的答案(如果有的话),以便将来的访问者知道什么有用!