使用nodejs爬取前程無憂前端技能排行,nodejs前程無憂

來源:互聯網
上載者:User

使用nodejs爬取前程無憂前端技能排行,nodejs前程無憂

最近準備換工作,需要更新一下技能樹。為做到有的放矢,想對招聘方的要求做個統計。正好之前瞭解過nodejs,所以做了個爬蟲搜尋資料。

具體步驟:

1.  先用fiddler分析請求需要的header和body。

2.  再用superagent構建上述資料發送用戶端請求。

3.  最後對返回的資料使用cheerio整理。

折騰了幾個晚上,只搞出了個架子,剩餘工作等有時間再繼續開發。

/*使用fiddler抓包,需要配置lan代理,且設定如下參數*/process.env.https_proxy = "http://127.0.0.1:8888";process.env.http_proxy = "http://127.0.0.1:8888";process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";/*使用到的模組*/var request = require('superagent');  //發送用戶端請求require('superagent-proxy')(request);  //使用代理髮送請求var cheerio = require('cheerio');    //以jq類似的方法操作返回的字元,不需要用正則require('superagent-charset')(request);//node不支援gbk,gb2312,this will add request.Request.prototype.charset.var async = require('async');      //非同步流量控制模組var fs = require('fs');/*相關參數,通過fiddler抓包後複製過來*/var ws = fs.createWriteStream('res.html',{flags:'w+'}); //a+追加的讀寫入模式,w+覆蓋var loginUrl = "https://login.51job.com/login.php";var searchUrl = "http://search.51job.com/jobsearch/search_result.php";var queryStrings = "fromJs=1&jobarea=020000&keyword=%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91&keywordtype=2&lang=c&stype=2&postchannel=0000&fromType=1&confirmdate=9";var loginForms = {  lang: 'c',  action: 'save',  from_domain: 'i',  loginname: '***',  //自己的使用者名稱和密碼  password: '***',  verifycode: '',  isread: 'on'};var searchForms = {  lang: 'c',  stype: '2',  postchannel: '0000',  fromType: '1',  line: '',  confirmdate: '9',  from: '',  keywordtype: '2',  keyword: '%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91',  jobarea: '020000',  industrytype: '',  funtype: ''};var searchFormsString='lang=c&stype=2&postchannel=0000&fromType=1&line=&confirmdate=9&from=&keywordtype=2&keyword=%C7%B0%B6%CB%BF%AA%B7%A2&jobarea=020000&industrytype=&funtype=';var proxy0 = process.env.https_proxy;var proxy = process.env.http_proxy;const agent = request.agent();     //agent()方法產生的執行個體會儲存cookie供後續使用request.post(loginUrl).proxy(proxy0).send(loginForms).end(function (err,res0) {  agent.post(searchUrl)    .proxy(proxy)          //proxy()方法需緊跟在method方法之後調用,否則fiddler抓不到資料包    .type('application/x-www-form-urlencoded')    .query(queryStrings)      //使用字串格式    .send(searchFormsString)    .charset('gbk')        //通過charset可知編碼字元格式設定,不設定會有亂碼    .end(function (err, res) {      /* 以下是處理返回資料的邏輯代碼*/      var $ = cheerio.load(res.text);  //res.text是返回的報文主體      async.each($('.el.title').nextAll('.el'), function(v, callback) {        //將多餘的內容刪除,保留崗位、公司連結        $(v).prepend($(v).find('.t1 a'));        $(v).find('.t1').remove();        ws.write($.html(v), 'utf8');      }, function(err) {        console.log(err);      });      console.log('successful');    })});//jquery內建document元素為root,cheerio需要通過load方法傳入,然後用選取器尋找指定元素,再執行相應操作。// $.html(el);靜態方法,返回el元素的outerHtml//TODO// 1.當前只請求到一頁資料,還需構建所有頁數的請求列表// 2.向每條資料的崗位連結發送請求,擷取技能關鍵字,存入檔案中// 3.node中io操作是非同步,且沒有鎖的概念,如何並發地向同一個檔案正確地寫入資料

結果顯示如下:

以上所述是小編給大家介紹的使用nodejs爬前程無憂前端技能排行,希望對大家有所協助,如果大家有任何疑問歡迎給我留言,小編會及時回複大家的!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.