java爬蟲(Jsoup)爬取某新聞站點標題
阿新 • • 發佈:2019-02-05
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點,五毛網的第一頁新聞的標題如下: