欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ruby+nokogori抓取糗事百科前10頁并存儲進數(shù)據(jù)庫示例

 更新時間:2014年05月04日 11:26:56   作者:  
這篇文章主要介紹了ruby+nokogori抓取糗事百科前10頁并存儲進數(shù)據(jù)庫示例,需要的朋友可以參考下

ruby,nokogori,爬取糗事百科最新的10頁加圖片比并同時保存進文本跟數(shù)據(jù)庫

復制代碼 代碼如下:

#encoding:utf-8
require "open-uri"
require "nokogiri"
require "mysql"
@dbh=Mysql.real_connect("localhost","root","dengli","pachong")
@file=open("qb.txt","w")
def getQiubai(url)
  data=open(url){|f| f.read}
  doc=Nokogiri::HTML(data)
  doc.css('div.block.untagged.mb15.bs2').each{|p|
    content=p.css('div.content').text
    @file.puts("#{content}\n")
    img=p.css('div.thumb img').each{|img|
       imgAddr=img.attr('src')
       @file.puts("#{imgAddr}\n")
       time=Time.now
       sql="INSERT INTO qiushibaike (`body`,`img`,`time`) VALUES ('#{content}','#{imgAddr}','#{time}')"
       @dbh.query(sql)
       }
  }
end
for i in 1..10
  @file.puts("這是第#{i}頁")
  url="http://www.qiushibaike.com/8hr/page/#{i}"
  getQiubai(url)
end

相關文章

最新評論