使用js抓取今日頭條的文章

來源:互聯網
上載者:User

標籤:介面   ajax   文章   爬蟲   今日頭條   

首先驗證頭條介面是否支援jsonp,在介面地址的參數中添加callback=call

http://www.toutiao.com/pgc/ma/?page_type=1&count=10&version=2&platform=pc&media_id=52155491628&max_behot_time=0&as=A1A578A48B28B39&cp=584B080BD3899E1&callback=call

提供者,返回的資料為:call(...)的形式

650) this.width=650;" src="http://www.weare.net.cn/EasyWeb/upload/image/20161210/1481347241034045188.png" title="1481347241034045188.png" alt="blob.png" style="border:0px;vertical-align:middle;height:auto;" />

由此判斷介面支援jsonp,那麼接下來就好辦了,可以使用jsonp提供者,然後從返回資料中提取出所需要的資料。

var time = 0;var as_param;var cp_param;$(function(){var param = getParam();as_param = param.as;cp_param = param.cp;getWeare();});function getWeare(){$.ajax({url:"http://www.toutiao.com/pgc/ma/?page_type=1&count=10&version=2&platform=pc&media_id=52155491628&max_behot_time="+time+"&as="+as_param+"&cp="+cp_param+"&callback=tt",type:‘get‘,dataType:‘jsonp‘,jsonp:‘tt‘});}function tt(data){var tarray = [];if(data.has_more==1){var adata = data.data;for(var i = 0; i < adata.length; i++){var tmap = {};var title = adata[i].title;var link = adata[i].source_url;tmap[‘title‘] = title;tmap[‘url‘] = TT + link;tarray.push(tmap);}}}

如此,搞定

詳細代碼請訪問微兒部落格,可百度搜尋微兒部落格或者直接點擊 www.weare.net.cn

本文出自 “12366658” 部落格,請務必保留此出處http://12376658.blog.51cto.com/12366658/1882128

使用js抓取今日頭條的文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.