需要一个包:jsoup-1.7.3.jar
有一定的java和js基础的人,一看就懂了!
一个不错的Jsoup中文文档下载地址:http://download.csdn.net/detail/apache2011/4517327
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
getWuMaoW();
}
//获取5毛网上的文章标题
public static void getWuMaoW(){
String url = "http://www.wumaow.com";
Document doc = null;
try {
doc = Jsoup.connect(url).get();
Elements listDiv = doc.getElementsByAttributeValue("class", "post");
for(Element element : listDiv){
Elements texts = element.getElementsByTag("h4");
for(Element text:texts){
String ptext = text.text();
System.out.println("标题:"+ptext);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2016-6-8 下午5点,五毛网的第一页新闻的标题如下:
下一遍 抓评论
地址:http://blog.csdn.net/disiwei1012/article/details/51678977