【问题标题】:Java Web Scraping wth Jaunt Library使用 Jaunt 库进行 Java Web Scraping
【发布时间】:2014-12-10 11:53:31
【问题描述】:

我遇到了 Java 网页抓取问题。这是网站:(http://www.bbc.com/sport/football/teams/liverpool)

我想从这里抓取标题数据。

注意:我正在使用 Jaunt 库。

public class News extends JFrame
{
    private String title;
    private JLabel labelText2;

    News()
    {

        setSize(800, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        labelText2 = new JLabel();
        setLayout(null);
        labelText2.setBounds(10, 10, 550, 20);
        add(labelText2);
    }

    public void Club_News()
    {
        try {

            UserAgent userAgent = new UserAgent();
            userAgent.visit("http://www.bbc.com/sport/football/teams/liverpool");
            String div = userAgent.doc.findFirst(
                    "<div id=\"more-headlines\" class=\"mod mod-separator\">").innerHTML();
            labelText2.setText("Latest News Headlines: " + div);
        } catch (JauntException e) {
            System.err.println(e);
        }
    }
}

【问题讨论】:

  • 需要包括确切问题的详细信息 - 还可以使用一些拼写检查和正确使用代码格式。
  • 仅供参考 - 您可以在 findFirst 方法的搜索查询中使用撇号而不是转义引号。

标签: java web-scraping jaunt-api


【解决方案1】:

您需要调用您的Club_News() 方法。您可以将其设为构造函数中的最后一条语句:

add(labelText2); // existing last line
Club_News(); // new method call

为了使错误更明显,在catch 块的Club_News() 末尾添加一行:

System.err.println(e); // existing error handling
labelText2.setText(e.toString()); // new hint

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 2017-05-14
    • 2020-01-16
    • 2018-11-11
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多