ruby,nokogori,爬取糗事百科最新的10頁(yè)加圖片比并同時(shí)保存進(jìn)文本跟數(shù)據(jù)庫(kù)
復(fù)制代碼 代碼如下:
#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}頁(yè)")
url="http://www.qiushibaike.com/8hr/page/#{i}"
getQiubai(url)
end
您可能感興趣的文章:- 淺談Ruby on Rails下的rake與數(shù)據(jù)庫(kù)數(shù)據(jù)遷移操作
- Ruby連接使用windows下sql server數(shù)據(jù)庫(kù)代碼實(shí)例
- 在Ruby程序中連接數(shù)據(jù)庫(kù)的詳細(xì)教程
- Ruby中訪問(wèn)SQL Server數(shù)據(jù)庫(kù)的配置實(shí)例
- Ruby on Rails框架程序連接MongoDB的教程