您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么使用Ruby和Nokogiri模擬爬蟲導(dǎo)出RSS種子,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
# encoding: utf-8 require 'thread' require 'nokogiri' require 'open-uri' require 'rss/maker' $result=Queue.new def extract_readme_header(no,name,url) frame = Nokogiri::HTML(open(url)) return unless frame readme=$url+frame.css('frame')[1]['src'] return unless readme open(readme) do |f| doc = Nokogiri::HTML(f.read) text=doc.css("div#content div#filecontents p")[0..4].map { |c| c.content }.join(" ").strip return if text.length==0 if text !~ /(rails)|(activ_)/i puts "========= #{no} #{name} : #{text[0..50]}" date = f.last_modified $result << [no,name,readme,date,text] end end rescue puts $!.to_s end def make_rss(items) RSS::Maker.make("2.0") do |m| m.channel.title = "GtitHub recently updated projects" m.channel.link = "http://localhost" m.channel.description = "GitHub recently updated projects" m.items.do_sort = true items.each do |no,name,url,date,descr| i = m.items.new_item i.title = name i.link = url i.description=descr i.date = date end end end ############################## M A I N ######################## ############# Scan list of recent project lth=[] $url="http://rdoc.info" puts "get url #{$url}..." doc = Nokogiri::HTML(open($url)) doc.css('ul.libraries')[1].css('li').each_with_index do |li,i| aname =li.css('a').first name=aname.content purl=$url+aname['href'] lth << Thread.new(i,name,purl) { |j,n,u| extract_readme_header(j,n,u) } end ################ wait all readme are read lth.each { |th| th.join() } ################ dequeue results and sort them by date descending result=[] result << $result.shift while $result.size>0 result.sort! { |a,b| a[0] <=> b[0] } ################ format results in rss File.open("RubyFeeds.rss","w") do |file| file.write make_rss(result) end
看完上述內(nèi)容,你們對(duì)怎么使用Ruby和Nokogiri模擬爬蟲導(dǎo)出RSS種子有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。