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

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 14:41   46   0

需要一个包: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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP