Open Err0rZero opened 7 years ago
哪个地方无法运行啊,主页能进去码
进不去,端口也没起
就这样,什么也没提示。
运行 rails server
启动rails服务器,我这步在readme中没写
我已经启动了,不好意思,没看到,还有个问题,json数据怎么导入?
我没有找到文档能让爬虫抓取下来的json数据存入数据库中,特像您请教下。谢谢
在db文件夹下,seeds.rb用来导入json到数据库中,你可以看这个文件的代码:
file = File.read('db/housedata.json')
file.gsub(/\{(.*?)\}/).each do |line|
hash=JSON.parse(line)
house=House.new
house.area=hash['area'][0].scan(/[0-9]/).join.to_i
house.url=hash['url'][0]
house.average_price=hash['average_price'][0].scan(/[0-9]/).join.to_i
house.floor=hash['floor'][0]
house.build_time=hash['build_time'][0].scan(/[0-9]/).join
house.community=hash['location'][0].delete(' ').scan(/\n(.*?)\n/).join.gsub("\u00A0", "")
house.street=hash['location'][0].delete(' ').scan(/\[(.*?)\]/).join
house.room_shape=hash['room_shape'][0]
house.save
end
这里就是把json文件导入数据库的代码
运行 rake db:seed
就能执行啊
出现了这个。。。
house.area=hash['area'][0].scan(/[0-9]/).join.to_i
hash里并没有读取到db/housedata.json中有关area字段的信息,你先看一下这个文件在不在,然后在解析这个文件的时候,对每一个需要解析的字段进行打印,确保解析顺利以后再往数据库里放
这个我已经改好了,估计是网页改过结构了,所以抓取到的数据和你的格式不一样造成了
file = File.read('db/housedata.json')
file.gsub(/\{(.*?)\}/).each do |line|
hash=JSON.parse(line)
house=House.new
house.area=hash['area'][0].scan(/[0-9]/).join.to_i
house.url=hash['url'][0]
#print hash['price'][0].scan(/[0-9]/).join.to_i
house.average_price=(hash['price'][0].scan(/[0-9]/).join.to_i * 10000) / hash['area'][0].scan(/[0-9]/).join.to_i #hash['average_price'][0].scan(/[0-9]/).join.to_i
house.floor=hash['floor'][0]
house.build_time=hash['build_time'][0].scan(/[0-9]/).join
#print hash['location'][0].delete(' ').scan(/\n(.*?)\n/).join.gsub("\u00A0", "")
house.community=hash['location'][0].delete(' ').scan(/\n(.*?)\n/).join.gsub("\u00A0", "")
house.street=hash['location'][0].delete(' ').scan(/\n(.*?)\n/).join.gsub("\u00A0", "")#hash['location'][0].delete(' ').scan(/\[(.*?)\]/).join
house.room_shape=hash['room_shape'][0]
house.save
end
我安装了也无法运行啊,没有报错