1

EDIT: My original question was way off, my apologies. Mark Reed has helped me find out the real problem, so here it is.

Note that this code works:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

source_url = "www.flickr.com"
puts "Visiting #{source_url}"
page = Nokogiri::HTML(open("http://website/script.php?value=#{source_url}"))
textarea = page.css('textarea') 


filename = source_url.to_s + ".txt"
create_file = File.open("#{filename}", 'w')
create_file.puts textarea
create_file.close

Which is really awesome, but I need it to do this to ~110 URLs, not just Flickr. Here's my loop that isn't working:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

File.open('sources.txt').each_line do |source_url|
  puts "Visiting #{source_url}"
  page = Nokogiri::HTML(open("http://website/script.php?value=#{source_url}"))
  textarea = page.css('textarea') 

  filename = source_url.to_s + ".txt"
  create_file = File.open("#{filename}", 'w')
  create_file.puts "#{textarea}"
  create_file.close
end

What am I doing wrong with my loop?

7
  • 1
    The correct syntax is File.open(variable, 'w'). what is the contents of variable? Commented May 5, 2012 at 1:43
  • I've added the full code - variable = source_url Commented May 5, 2012 at 1:50
  • What is the value when it fails, though? Can you create a file with that name in that folder via other means? Commented May 5, 2012 at 2:15
  • I believe it's "www.flickr.com". Creating "www.flickr.com.txt" works perfectly fine manually. Commented May 5, 2012 at 2:17
  • but there's no '.txt' above. try just doing File.open("www.flickr.com","w") from irb and see what you get. I suspect Windows just doesn't like that as a filename. But if that works, it's probably time for a chat instead of more comments. Commented May 5, 2012 at 2:28

2 Answers 2

1

Ok, now you're looping over the lines of the input file. When you do that, you get strings that end in a newilne. So you're trying to create a file with a newline in the middle of its name, which is not legal in Windows.

Just chomp the string:

File.open('sources.txt').each_line do |source_url|
  source_url.chomp!
  # ... rest of code goes here ...

You can also use File#foreach instead of File#open.each_line:

File.foreach('sources.txt') do |source_url|
  source_url.chomp!
  # ... rest of code goes here 
Sign up to request clarification or add additional context in comments.

1 Comment

Low-tech way to debug these sorts of problems is just to add puts - you were printing out the URL, but you would have seen the problem right away if you printed out the actual entire filename right before the open call.
0

You're putting your parentheses in the wrong place:

create_file = File.open(variable, 'w')

1 Comment

Thanks. Changed. Still getting the same error though. Edited post to contain full code, maybe this isn't the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.