【发布时间】:2017-12-03 17:45:39
【问题描述】:
我一直在努力尝试使用 .uniq 方法从网站(在 /informatics 路径内)生成唯一的 URL 列表。无论我尝试什么,在尝试生成列表时都会出现方法错误。我确定这是语法问题,我希望有人能指出我正确的方向。
获得列表后,我需要通过 ActiveRecord 将它们存储到数据库中,但我需要唯一的列表,然后才能开始解决这个问题。
require 'nokogiri'
require 'open-uri'
require 'active_record'
ARGV[0]="https://www.nku.edu/academics/informatics.html"
ARGV.each do |arg|
open(arg) do |f|
# Display connection data
puts "#"*25 + "\nConnection: '#{arg}'\n" + "#"*25
[:base_uri, :meta, :status, :charset, :content_encoding,
:content_type, :last_modified].each do |method|
puts "#{method.to_s}: #{f.send(method)}" if f.respond_to? method
end
# Display the href links
base_url = /^(.*\.nku\.edu)\//.match(f.base_uri.to_s)[1]
puts "base_url: #{base_url}"
Nokogiri::HTML(f).css('a').each do |anchor|
href = anchor['href']
# Make Unique
if href =~ /.*informatics/
puts href
#store stuff to active record
end
end
end
end
【问题讨论】:
-
它有效,我得到gist.github.com/vnhnhm/f6f8079c7180b6b7ffeb52d23b563c3e 加上我省略的其他初始内容,具体问题是什么?
-
我知道该代码有效,但有一个重复的 URL。我需要确保没有任何重复