我正在编写一个测试脚本,打开一个文件,其中包含一个没有"www"和"com"的URL列表.
我试图读取每一行并将该行放入URL.然后我检查它是否重定向甚至存在.
我的问题是当我从文件中读取行并将其分配给变量时.然后,我在加载后与URL中的内容进行比较,以及我最初放在那里的内容,但它似乎是在我的变量之后添加一个返回.
基本上它总是说重定向,因为它放了" http://www.line \n.com/".
我该如何摆脱"\n"?
counter = 1
file = File.new("Data/activeSites.txt", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
browser.goto("http://www." + line + ".com/")
if browser.url == "http://www." + line + ".com/"
puts "Did not redirect"
else
puts ("Redirected to " + browser.url)
#puts ("http://www." + line + ".com/")
puts "http://www.#{line}.com/"
end
Run Code Online (Sandbox Code Playgroud)
基本上它总是说重定向因为它放了http://www.line然后返回.com /
我怎样才能摆脱回报呢?
简短回答: strip
"text\n ".strip # => "text"
Run Code Online (Sandbox Code Playgroud)
答案很长:
你的代码不是很像ruby,可以重构.
# Using File#each_line, the line will not include the newline character
# Adding with_index will add the current line index as a parameter to the block
File.open("Data/activeSites.txt").each_line.with_index do |line, counter|
puts "#{counter + 1}: #{line}"
# You're using this 3 times already, let's make it a variable
url = "http://#{line}.com"
browser.goto(url)
if browser.url == url
puts "Did not redirect"
else
puts ("Redirected to " + browser.url)
puts url
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2791 次 |
| 最近记录: |