ruby,nokogori,爬取糗事百科最新的10页加图片比并同时保存进文本跟数据库

复制代码 代码如下:
#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

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!