如何按类别…范围或其他方式过滤条目?
如何通过类别过滤我的条目?
我正在为了教育目的而使用reddit进行网页抓取,只抓取他们的标题,链接和类别。
这是通过lib目录的代码:
require 'open-uri' module RedditScrapper def self.scrape doc = Nokogiri::HTML(open("https://www.reddit.com/")) entries = doc.css('.entry') entries.each do |entry| title = entry.css('p.title > a').text link = entry.css('p.title > a')[0]['href'] category = entry.css('p.tagline > a.subreddit')[0]['href'] Entry.create!(title: title, link: link, category: category ) end end end
试图在index.html.erb
对此进行过滤,但不知道如何。
决定尝试范围,即使我以前从未写过。
这是Entry
模型中的范围
class Entry (category) { where category: category } end
将范围放在entries_controller
的索引操作上
class EntriesController < ApplicationController def index @entries = Entry.all @entries = @entries.categorization end def scrape RedditScrapper.scrape respond_to do |format| format.html { redirect_to entries_url, notice: 'Entries were successfully scraped.' } format.json { entriesArray.to_json } end end end
收到此错误消息说明:
ArgumentError in EntriesController#index wrong number of arguments (given 0, expected 1)
这是参考范围。
如何解决这个问题,或者按类别过滤条目,如果有不同的方式而不是范围。
在此先感谢,如果需要其他数据,请告诉我。
输出样本
LifeProTips LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. /r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/
你不需要这里的范围,它们用于清理where
子句。 只需使用group_by
def index @entries = Entry.all.group_by{ |entry| entry.category } # a shorter more ruby way of writing this is: # @entries = Entry.all.group_by(&:category) end
这将创建一个像这样的哈希:
{ "category1" => [Entry,Entry,Entry], "category2" => [Entry], ... }
然后在你看来你可以
<% @entries.each do |category, entries| %> <%= category %>
<% entries.each do |entry| %> <%= entry.inspect %>
<% end %> <% end %>
你的第二个问题是没有处理刮刀中的重复:
更改:
Entry.create!(title: title, link: link, category: category)
至:
Entry.where(title: title, link: link, category: category).first_or_create