Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法运行 #6

Open
Err0rZero opened this issue Jun 27, 2017 · 10 comments
Open

无法运行 #6

Err0rZero opened this issue Jun 27, 2017 · 10 comments

Comments

@Err0rZero
Copy link

我安装了也无法运行啊,没有报错

@PENGZhaoqing
Copy link
Owner

哪个地方无法运行啊,主页能进去码

@Err0rZero
Copy link
Author

进不去,端口也没起

@Err0rZero
Copy link
Author

image
就这样,什么也没提示。

@PENGZhaoqing
Copy link
Owner

运行 rails server 启动rails服务器,我这步在readme中没写

@Err0rZero
Copy link
Author

我已经启动了,不好意思,没看到,还有个问题,json数据怎么导入?

@Err0rZero
Copy link
Author

我没有找到文档能让爬虫抓取下来的json数据存入数据库中,特像您请教下。谢谢

@PENGZhaoqing
Copy link
Owner

在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 就能执行啊

@Err0rZero
Copy link
Author

image
出现了这个。。。

@PENGZhaoqing
Copy link
Owner

house.area=hash['area'][0].scan(/[0-9]/).join.to_i

hash里并没有读取到db/housedata.json中有关area字段的信息,你先看一下这个文件在不在,然后在解析这个文件的时候,对每一个需要解析的字段进行打印,确保解析顺利以后再往数据库里放

@Err0rZero
Copy link
Author

这个我已经改好了,估计是网页改过结构了,所以抓取到的数据和你的格式不一样造成了

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants