2012-02-12 58 views
0

這裏是我的代碼..保存一系列頁面的腳本然後嘗試將它們合併,但只合並一個頁面?

require "open-uri" 

base_url = "http://en.wikipedia.org/wiki" 

(1..5).each do |x| 
    # sets up the url 
    full_url = base_url + "/" + x.to_s 
    # reads the url 
    read_page = open(full_url).read 
    # saves the contents to a file and closes it 
    local_file = "my_copy_of-" + x.to_s + ".html" 
    file = open(local_file,"w") 
    file.write(read_page) 
    file.close 

    # open a file to store all entrys in 

    combined_numbers = open("numbers.html", "w") 

    entrys = open(local_file, "r") 

    combined_numbers.write(entrys.read) 

    entrys.close 
    combined_numbers.close 

end 

正如你所看到的。它基本上將維基百科文章1到5的內容進行了刪減,然後嘗試將它們合併到一個名爲numbers.html的文件中。

它做的第一點是正確的。但是,當它達到第二。它似乎只是寫在循環中的第五篇文章的內容。

雖然我看不出錯在哪裏。任何幫助?

回答

2

打開摘要文件時選擇了錯誤的mode「w」覆蓋現有文件,而「a」附加到現有文件。與環numbers.html的文件內容寫入當前文章的每遍否則

combined_numbers = open("numbers.html", "a") 

所以用這個來讓你的代碼工作。


此外,我認爲你應該使用在read_page內容寫入numbers.html而不是從你剛寫入的文件在閱讀他們回來:

require "open-uri" 

(1..5).each do |x| 
    # set up and read url 
    url = "http://en.wikipedia.org/wiki/#{x.to_s}" 
    article = open(url).read 

    # saves current article to a file 
    # (only possible with 1.9.x use open too if on 1.8.x) 
    IO.write("my_copy_of-#{x.to_s}.html", article) 

    # add current article to summary file 
    open("numbers.html", "a") do |f| 
    f.write(article) 
    end 
end 
相關問題