所以我有一个方法,当我传入一个直接的URL(见下面的代码)时,该方法返回正常。当我使用#{}
传递URL以获得在Craigslist中使用不同位置的可能性时,它会抛出底部显示的错误。我想我的问题是双重的:
代码:
def get_post_date(listing_url)
# This method takes in a page and returns a date hopefully in a date format
# but right now text
listing = Nokogiri::HTML(open(listing_url)).css("p")
setter = ""
for element in listing
if element.css('time').text!=""&&setter==""
post_time = "poop" # Time.parse(element.css('time').text)
return "poop"
end
end
end
location = "sfbay"
# THIS throws an error
p get_post_date("#{location}.craigslist.org/sfc/vac/4248712420.html")
# THIS works
p get_post_date("sfbay.craigslist.org/sfc/vac/4248712420.html")
错误:
c:\>ruby cljobs.rb C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:35:in `initialize': No such file or direct ory - sfbay.craigslist.org/sfc/vac/4248712420.html (Errno::ENOENT) from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:35:in `open' from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:35:in `open' from cljobs.rb:7:in `get_post_date' from cljobs.rb:40:in `'
答案 0 :(得分:2)
要打开网址,您需要使用OpenURI。否则nokogiri会尝试打开一个文件。
require 'open-uri'
listing = Nokogiri::HTML(open(listing_url))