java爬虫(Jsoup)爬取某新闻站点标题

需要一个包:jsoup-1.7.3.jar 
有一定的java和js基础

package wang.test;

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 JsoupTestTitle {
	public static void main(String[] args) throws Exception {
		getWuMaoW();
	}

	// 获取http://www.ltaaa.com/
	public static void getWuMaoW() {
		String url = "http://www.ltaaa.com/";
		Document doc = null;
		try {
			doc = Jsoup.connect(url).get();
			Elements listDiv = doc.getElementsByAttributeValue("class", "show");
			for (Element element : listDiv) {
				Elements texts = element.getElementsByTag("a");
				for (Element text : texts) {

					// 取所有文本
					// String ptext = text.text();

					String ptext = text.attr("title");
					System.out.println("标题:" + ptext);

				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 java爬虫(Jsoup)爬取某新闻站点标题_第1张图片

 

你可能感兴趣的:(java,爬虫demo)