如何使用 Nokogiri 向文件中添加行?
How to add lines to a file with Nokogiri?
我正在尝试使用 Nokogiri 将解析后的信息添加到文本文件中。我想将每个新字符串附加到现有文件。
divs_with_links = uni_page.css('div.border-top_lightgrey')
divs_with_links.each_with_index do |div, index|
if div.css('h3').text == 'Biology'
uni_link = div.css("span.external-url").text || "no link found"
uni_email = div.css("p.email").text || "no email found"
data = "#{current_uni_title}|#{uni_link}|#{uni_email}"
open("uni.csv", "w"){ |file| file.write(data)}
break
end
end
每次我解析新页面时,Nokogiri 都会删除文件内容。
你打错了mode opening a file:
- open("uni.csv", "w"){ |file| file.write(data)}
# ⇓ appends, not overrides content
+ open("uni.csv", "a"){ |file| file.write(data)}
我正在尝试使用 Nokogiri 将解析后的信息添加到文本文件中。我想将每个新字符串附加到现有文件。
divs_with_links = uni_page.css('div.border-top_lightgrey')
divs_with_links.each_with_index do |div, index|
if div.css('h3').text == 'Biology'
uni_link = div.css("span.external-url").text || "no link found"
uni_email = div.css("p.email").text || "no email found"
data = "#{current_uni_title}|#{uni_link}|#{uni_email}"
open("uni.csv", "w"){ |file| file.write(data)}
break
end
end
每次我解析新页面时,Nokogiri 都会删除文件内容。
你打错了mode opening a file:
- open("uni.csv", "w"){ |file| file.write(data)}
# ⇓ appends, not overrides content
+ open("uni.csv", "a"){ |file| file.write(data)}