Ruby - 读取 csv 文件并在循环中执行值会跳过 csv 文件中的行
我确信这是一个完全无知的问题,但就这样吧。以下代码的目标是从标准 csv 文件中读取 id 列表,使用该值附加到 URL,调用 URL 并通过 xpath 提取特定属性。我遇到的问题是循环似乎跳过了一些行。
例如,下面是 10 个值的示例:
777961
777972
781033
781044
781055
847066
744187
893908
369009
369010
代码仅每隔一行读取一次。实际文件大约有 6000 行,不算大,但我只得到第二个文件中返回的大约 2500 个值。
f = File.open('test.csv', 'r+')
url_f = File.open("url.csv", "w")
for line in f
f.each_line do |item|
item = f.gets
url = "http://test.com/testid=" + item
client = HTTPClient.new
resp = client.get_content(url)
doc = Nokogiri::HTML(resp)
doc.xpath("//link[@rel='canonical']/@href").each do |attr|
url_f.puts attr.value
puts attr.value
end
puts item
end
end
I'm sure this is a completely ignorant question but here it goes. The following code's objective is to read a list of id's from a standard csv file, use the value to append to a URL, call the URL and extract a specific attribute via xpath. The problem I'm having is that the loop seems to be skipping some lines.
In example, here is a sample of 10 values:
777961
777972
781033
781044
781055
847066
744187
893908
369009
369010
The code is only reading every other line. The actual file has around 6000 lines, not huge but I'm only getting about 2500 values returned in the second file.
f = File.open('test.csv', 'r+')
url_f = File.open("url.csv", "w")
for line in f
f.each_line do |item|
item = f.gets
url = "http://test.com/testid=" + item
client = HTTPClient.new
resp = client.get_content(url)
doc = Nokogiri::HTML(resp)
doc.xpath("//link[@rel='canonical']/@href").each do |attr|
url_f.puts attr.value
puts attr.value
end
puts item
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我已经弄清楚了。
我有一行
item = f.gets
,每次循环运行时都会调用下一行,从而跳过所有其他行。我知道这是一个菜鸟问题。 :PNevermind, I figured it out.
I had the line
item = f.gets
which would call the next line every time the loop ran thus skipping every other line. I knew it was a noob question. :P