我是JSoup的新手,如果我的问题太琐碎了,很抱歉。我试图从http://www.nytimes.com/中提取文章文本,但在打印解析文档时,在解析输出中看不到任何文章
public class App
{
public static void main( String[] args )
{
String url = "http://www.nytimes.com/";
Document document;
try {
document = Jsoup.connect(url).get();
System.out.println(document.html()); // Articles not getting printed
//System.out.println(document.toString()); // Same here
String title = document.title();
System.out.println("title : " + title); // Title is fine
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 好的,我尝试解析"http://en.wikipedia.org/wiki/Big_data“来检索维基数据,这里也有同样的问题,我没有得到输出中的维基数据。任何帮助或提示都将不胜感激。
谢谢。
发布于 2013-06-21 21:34:15
下面是如何获取所有<p class="summary>文本的方法:
final String url = "http://www.nytimes.com/";
Document doc = Jsoup.connect(url).get();
for( Element element : doc.select("p.summary") )
{
if( element.hasText() ) // Skip those tags without text
{
System.out.println(element.text());
}
}如果你需要所有的<p>标签,而不需要任何过滤,你可以使用doc.select("p")。但在大多数情况下,最好只选择您需要的那些(参见here中的Jsoup Selector文档)。
https://stackoverflow.com/questions/17194499
复制相似问题