0

我从validate_pdf_links()返回数组的方法调用中获取了一组链接:

broken_pdf_links = broken_pdf_links + validate_pdf_links()

我在具有多个链接的多个页面上执行此操作,因此它的工作原理如下:

  1. 转到页面。
  2. 使用 . 检查该页面上的所有链接validate_pdf_links()
  3. 转到下一页。
  4. 重复。

我想知道链接断开时我在哪个页面上,因此我使用以下内容进行页面识别:

page_name = @browser.title

我想要一个输出如下:

broken_pdf_links.each do |pdf|
puts "The pdf link #{pdf} was broke on page #{page_name}

我应该如何组装散列或数组以包含元素并以我建议的方式检索它们?

编辑:::::: 目前正在尝试:

broken_pdf_links.each {|pdf| broken_pdfs = broken_pdfs.push[pdf,page_name]}

存储,然后

broken_pdfs.each do |broke_pdf|
   puts "Link #{broke_pdf[0][0]} on Page #{broke_pdf[0][1]}\n"
   pdf_index += 1
end

读书

但我在 store 命令中收到错误“TypeError:无法将数组转换为整数”。

4

2 回答 2

1

每个页面上可能有多个断开的链接。哈希在这里会很好。

broken_pdf_links = {}

broken_pdf_links.merge!(:page_name => validate_pdf_links())

broken_pdf_links.each do |k,v|
  puts "The pdf link(s) #{v} was broken on page #{k}"
end

Output:

The pdf link(s) ["fail1", "fail2", "fail3"] was broken on page page_name

The pdf link(s) ["fail1"] was broken on page other_page

You could go further with the array and format it.

于 2013-01-08T21:15:43.003 回答
1

Instead of:

broken_pdf_links.each {|pdf| broken_pdfs = broken_pdfs.push[pdf,page_name]}

Use:

broken_pdfs = broken_pdf_links.map { |pdf| [pdf,page_name] }

Mostly you were mixing up several ways of aggregating the sub-array.

You could use map, which is shorter and more direct, or:

broken_pdfs = []
broken_pdf.links.each do |pdf|
  broken_pdfs += [pdf, page_name]
end

An alternate to that is:

broken_pdfs = []
broken_pdf.links.each do |pdf|
  broken_pdfs.push([pdf, page_name])
end
于 2013-01-08T21:18:48.723 回答