我遇到了 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);
}
}
}
最佳答案
您需要调用您的 Club_News()
方法。您可以将其作为构造函数中的最后一个语句:
add(labelText2); // existing last line
Club_News(); // new method call
为了使错误明显,请在 Club_News()
末尾的 catch
block 中添加一行:
System.err.println(e); // existing error handling
labelText2.setText(e.toString()); // new hint
关于使用 Jaunt 库进行 Java 网页抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400350/