我如何使用 Jaunt 库从网站上抓取数据?
How can I scrape data from a website using the Jaunt library?
我想从这个网站获取标题:http://feeds.foxnews.com/foxnews/latest
像这个例子:
<title><![CDATA[SUCCESSFUL INTERCEPT Pentagon confirms it shot down ICBM-type target]]></title>
它会显示这样的文字:
“拦截成功 五角大楼确认击落 ICBM-type 目标
五角大楼表示,美国进行了成功的导弹拦截试验
这是我的代码。我用过 jaunt 库。
我不知道为什么只显示文字"foxnew.com"
import com.jaunt.JauntException;
import com.jaunt.UserAgent;
public class p8_1
{
public static void main(String[] args)
{
try
{
UserAgent userAgent = new UserAgent();
userAgent.visit("http://feeds.foxnews.com/foxnews/latest");
String title = userAgent.doc.findFirst
("<title><![CDATA[SUCCESSFUL INTERCEPT Pentagon confirms it shot down ICBM-type target]]></title>").getText();
System.out.println("\n " + title);
} catch (JauntException e)
{
System.err.println(e);
}
}
}
搜索元素类型,而不是值。
尝试以下方法获取 Feed 中每个项目的标题文本:
public static void main(String[] args) {
try {
UserAgent userAgent = new UserAgent();
userAgent.visit("http://feeds.foxnews.com/foxnews/latest");
Elements items = userAgent.doc.findEach("<item>");
Elements titles = items.findEach("<title>");
for (Element title : titles) {
String titleText = title.getComment(0).getText();
System.out.println(titleText);
}
} catch (JauntException e) {
System.err.println(e);
}
}
我想从这个网站获取标题:http://feeds.foxnews.com/foxnews/latest
像这个例子:
<title><![CDATA[SUCCESSFUL INTERCEPT Pentagon confirms it shot down ICBM-type target]]></title>
它会显示这样的文字:
“拦截成功 五角大楼确认击落 ICBM-type 目标 五角大楼表示,美国进行了成功的导弹拦截试验
这是我的代码。我用过 jaunt 库。
我不知道为什么只显示文字"foxnew.com"
import com.jaunt.JauntException;
import com.jaunt.UserAgent;
public class p8_1
{
public static void main(String[] args)
{
try
{
UserAgent userAgent = new UserAgent();
userAgent.visit("http://feeds.foxnews.com/foxnews/latest");
String title = userAgent.doc.findFirst
("<title><![CDATA[SUCCESSFUL INTERCEPT Pentagon confirms it shot down ICBM-type target]]></title>").getText();
System.out.println("\n " + title);
} catch (JauntException e)
{
System.err.println(e);
}
}
}
搜索元素类型,而不是值。
尝试以下方法获取 Feed 中每个项目的标题文本:
public static void main(String[] args) {
try {
UserAgent userAgent = new UserAgent();
userAgent.visit("http://feeds.foxnews.com/foxnews/latest");
Elements items = userAgent.doc.findEach("<item>");
Elements titles = items.findEach("<title>");
for (Element title : titles) {
String titleText = title.getComment(0).getText();
System.out.println(titleText);
}
} catch (JauntException e) {
System.err.println(e);
}
}