java簡(jiǎn)單的爬蟲(chóng)(今日頭條)

之前在做資訊站的時(shí)候需要用到爬蟲(chóng)來(lái)獲取一些文章,今天剛好有空就研究了一下.在網(wǎng)上看到了一個(gè)demo,使用的是Jsoup,我拿過(guò)來(lái)修改了一下,
由于今日頭條的文章的特殊性,所以無(wú)法直接獲取文章的地址,需要獲取文章的id然后在拼接成url再訪問(wèn).

public class Demo2 {

 public static void main(String[] args) {

    // 需要爬的網(wǎng)頁(yè)的文章列表
    String url = "http://www.toutiao.com/news_finance/";
    //文章詳情頁(yè)的前綴(由于今日頭條的文章都是在group這個(gè)目錄下,所以定義了前綴,而且通過(guò)請(qǐng)求獲取到的html頁(yè)面)
    String url2="http://www.toutiao.com/group/";
    //鏈接到該網(wǎng)站
    Connection connection = Jsoup.connect(url);
    Document content = null;
    try {
        //獲取內(nèi)容
        content = connection.get();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //轉(zhuǎn)換成字符串
    String htmlStr = content.html();
    //因?yàn)榻袢疹^條的文章展示比較奇葩,都是通過(guò)js定義成變量,所以無(wú)法使用獲取dom元素的方式獲取值
    String jsonStr = StringUtils.substringBetween(htmlStr,"var _data = ", ";");
    System.out.println(jsonStr);
    Map parse = (Map) JSONObject.parse(jsonStr);
    JSONArray parseArray = (JSONArray) parse.get("real_time_news");
    Map map=null;
    List<Map> maps=new ArrayList<>();
    //遍歷這個(gè)jsonArray,獲取到每一個(gè)json對(duì)象,然后將其轉(zhuǎn)換成Map對(duì)象(在這里其實(shí)只需要一個(gè)group_id,那么沒(méi)必要使用map)
    for(int i=0;i<parseArray.size();i++){
        map = (Map)parseArray.get(i);
        maps.add((Map)parseArray.get(i));
        System.out.println(map.get("group_id"));
        
    }
    //遍歷之前獲取到的map集合,然后分別訪問(wèn)這些文章詳情頁(yè)
    for (Map map2 : maps) {
        connection = Jsoup.connect(url2+map2.get("group_id"));
        try {
            Document document = connection.get();
            //獲取文章標(biāo)題
            Elements title = document.select("[class=article-title]");
            System.out.println(title.html());
            //獲取文章來(lái)源和文章發(fā)布時(shí)間
            Elements articleInfo = document.select("[class=articleInfo]");
            Elements src = articleInfo.select("[class=src]");
            System.out.println(src.html());
            Elements time = articleInfo.select("[class=time]");
            System.out.println(time.html());
            //獲取文章內(nèi)容
            Elements contentEle = document.select("[class=article-content]");
            System.out.println(contentEle.html());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評(píng)論 25 709
  • 爬蟲(chóng)文章 in 簡(jiǎn)書(shū)程序員專題: like:128 - Python 爬取落網(wǎng)音樂(lè) like:127 - 【圖文詳...
    treelake閱讀 29,746評(píng)論 33 638
  • 今天咱來(lái)說(shuō)說(shuō)關(guān)于古典的一些事情。 先引用一下定義,古典這個(gè)定義就是:以一種嚴(yán)肅考究,并且含有一定的形式...
    煙老師閱讀 399評(píng)論 0 0
  • 人老了,就像這樹(shù)上的葉子,終究是要凋零的。
    簡(jiǎn)記微語(yǔ)閱讀 215評(píng)論 0 0
  • 其實(shí)畫(huà)畫(huà)可以讓心很靜,即使畫(huà)的不夠好,也是畫(huà)的一種心情。 繪畫(huà)最主要的要有耐心,畫(huà)多了,每個(gè)人都是大師...
    快樂(lè)的Alina閱讀 431評(píng)論 2 2

友情鏈接更多精彩內(nèi)容