Node.js Web 爬蟲:Node Osmosis

n6xb 9年前發布 | 63K 次閱讀 網絡爬蟲 Node Osmosis

Osmosis 是 Node.js 用來解析 HTML/XML 和 Web 內容爬取的擴展。

Features

  • Fast: uses libxml C bindings
  • Lightweight: no dependencies like jQuery, cheerio, or jsdom
  • Clean: promise based interface- no more nested callbacks
  • Flexible: supports both CSS and XPath selectors
  • Predictable: same input, same output, same order
  • Detailed logging for every step
  • Precise and natural IO flow- no setTimeout or process.nextTick
  • Easy debugging with built-in stack size and memory usage reporting
  • Memory leak free

Example: scrape all craigslist listings

var osmosis = require('osmosis');

osmosis
.get('www.craigslist.org/about/sites') 
.find('h1 + div a')
.set('location')
.follow('@href')
.find('header + div + div li > a')
.set('category')
.follow('@href')
.find('p > a', '.totallink + a.button.next:first')
.follow('@href')
.set({
    'title':        'section > h2',
    'description':  '#postingbody',
    'subcategory':  'div.breadbox > span[4]',
    'date':         'time@datetime',
    'latitude':     '#map@data-latitude',
    'longitude':    '#map@data-longitude',
    'images[]':     'img@src'
})
.data(function(listing) {
    // do something with listing data
})

項目主頁:http://www.baiduhome.net/lib/view/home/1428322356791

 本文由用戶 n6xb 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!