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