我正试图通过带有制造吉普车的车辆来过滤我的佣金任务。我写过:if posting["make"] = "jeep"
但似乎所有的汽车数据都被拉了,而不仅仅是带有品牌"jeep"
的汽车....
"jeep"
是从API中提取的数据的注释值。在运行这个rake任务时,我得到所有车辆制造,包括吉普车。
namespace :scraper do
desc "Fetch Craigslist posts from 3Taps"
task scrape: :environment do
require 'open-uri'
require 'json'
# Set API token and URL
auth_token = "XXXXXXXXXXXXXXX"
polling_url = "http://polling.3taps.com/poll"
# Specify request parameters
params = {
auth_token: auth_token,
anchor: 1900885490,
source:"CRAIG",
category_group: "VVVV",
category: "VAUT",
'location.city' => "USA-PIT-PIT",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
# Prepare API request
uri = URI.parse(polling_url)
uri.query = URI.encode_www_form(params)
# Submit request
result = JSON.parse(open(uri).read)
# Display results to screen
#puts JSON.pretty_generate result["postings"].first
#end
# #store results in Database
result["postings"].each do |posting|
#ADD HARD FILTER (IN PROGRESS....)
if posting["make"] == "Jeep"
#create new post
@post= Post.new
@post.heading = posting["heading"]
@post.body = posting["body"]
@post.price = posting["price"]
@post.neighborhood = Location.find_by(code: posting["location"]["locality"]).try(:name)
@post.external_url = posting["external_url"]
@post.timestamp = posting["timestamp"]
@post.year = posting ["annotations"]["year"] if posting ["annotations"]["year"].present?
@post.make = posting ["annotations"]["make"] if posting ["annotations"]["make"].present?
@post.model = posting ["annotations"]["model"] if posting ["annotations"]["model"].present?
@post.title_status = posting ["annotations"]["title_status"] if posting ["annotations"]["title_status"].present?
@post.transmission = posting ["annotations"]["transmission"] if posting ["annotations"]["transmission"].present?
@post.mileage = posting ["annotations"]["mileage"] if posting ["annotations"]["mileage"].present?
#Save Post
@post.save
#Loop over images and save to image database
posting["images"].each do |image|
@image = Image.new
@image.url = image["full"]
@image.post_id = @post.id
@image.save
end
end
end
desc "Destroy All Posting Data"
task destroy_all_posts: :environment do
Post.destroy_all
end
###save neighborhoods
desc "Save neighborhood codes in a reference table"
task scrape_neighborhoods: :environment do
require 'open-uri'
require 'json'
# Set API token and URL
auth_token = "XXXXXXXXXXXXXXX"
location_url = "http://reference.3taps.com/locations"
# Specify request parameters
params = {
auth_token: auth_token,
level: "locality",
city: "USA-PIT-PIT"
}
# Prepare API request
uri = URI.parse(location_url)
uri.query = URI.encode_www_form(params)
# Submit request
result = JSON.parse(open(uri).read)
# Display results to screen
#puts JSON.pretty_generate result
# store results in Database
result["locations"].each do |location|
@location = Location.new
@location.code = location["code"]
@location.name = location["short_name"]
@location.save
end
end
end
end
答案 0 :(得分:1)
问题是您使用的是赋值运算符=
,而不是==
中的平等比较if posting["make"] = "Jeep"
。
之后,您需要将if更改为if posting["annotations"]["make"] == "jeep"
,因为服务器返回的数据是嵌套的。密钥make
位于annotations
。
答案 1 :(得分:0)
namespace :scraper do
desc "Fetch Craigslist posts from 3Taps"
task scrape: :environment do
require 'open-uri'
require 'json'
# Set API token and URL
auth_token = "@@"
polling_url = "http://polling.3taps.com/poll"
# Specify request parameters
params = {
auth_token: auth_token,
anchor: 1900885490,
source:"CRAIG",
category_group: "VVVV",
category: "VAUT",
'location.city' => "USA-PIT-PIT",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
# Prepare API request
uri = URI.parse(polling_url)
uri.query = URI.encode_www_form(params)
# Submit request
result = JSON.parse(open(uri).read)
# Display results to screen
#puts result["postings"].first["annotations"]["year"]
# #store results in Database
result["postings"].each do |posting|
if posting["annotations"]["make"] == "Jeep"
#create new post
@post= Post.new
@post.heading = posting["heading"]
@post.body = posting["body"]
@post.price = posting["price"]
@post.neighborhood = posting["location"]["locality"]
@post.external_url = posting["external_url"]
@post.timestamp = posting["timestamp"]
@post.year = posting ["annotations"]["year"] if posting ["annotations"]["year"].present?
@post.make = posting ["annotations"]["make"] if posting ["annotations"]["make"].present?
@post.model = posting ["annotations"]["model"] if posting ["annotations"]["model"].present?
@post.title_status = posting ["annotations"]["title_status"] if posting ["annotations"]["title_status"].present?
@post.transmission = posting ["annotations"]["transmission"] if posting ["annotations"]["transmission"].present?
@post.mileage = posting ["annotations"]["mileage"] if posting ["annotations"]["mileage"].present?
#Save Post
@post.save
# Loop over images and save to Image database
posting["images"].each do |image|
@image = Image.new
@image.url = image["full"]
@image.post_id = @post.id
@image.save
end
end
end
end
desc "Destroy All Posting Data"
task destroy_all_posts: :environment do
Post.destroy_all
end
end