2014-07-18 28 views
0

Ruby n00b這裏。我將同一頁面重複兩次 - 但每次都以稍微不同的方式 - 將其導出到單獨的CSV文件。然後,我想結合CSV第1號的第一列和CSV第2號的第2列來創建CSV第3號。ruby​​/nokogiri scraping - 導出到多個CSV,然後從每個CSV中取出列並結合成最終的CSV

拖動CSVs的代碼NO.1 & 2作品。但添加我的嘗試將兩個CSV組合成第三個(在底部註釋掉)返回以下錯誤 - 兩個CSV填充正常,但第三個保持空白,並且腳本處於似乎是無限循環的狀態。我知道這行不應該是在底部,但我看不出有什麼地方它會去...

alts.rb:45:in `block in <main>': undefined local variable or method `scrapedURLs1' for main:Object (NameError) 
    from /Users/JammyStressford/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/csv.rb:1266:in `open' 
    from alts.rb:44:in `<main>' 

代碼本身:

require 'rubygems' 
require 'nokogiri' 
require 'open-uri' 
require 'csv' 


url = "http://www.example.com/page" 
page = Nokogiri::HTML(open(url)) 


CSV.open("results1.csv", "wb") do |csv| 
    page.css('img.product-card-image').each do |scrape| 
    product1 = scrape['alt'] 
    page.css('a.product-card-image-link').each do |scrape| 
     link1 = scrape['href'] 

     scrapedProducts1 = "#{product1}"[0..-7] 
     scrapedURLs1 = "{link1}" 

     csv << [scrapedProducts1, scrapedURLs1] 
    end 
    end 
end 

CSV.open("Results2.csv", "wb") do |csv| 
    page.css('a.product-card-image-link').each do |scrape| 
    link2 = scrape['href'] 
    page.css('img.product-card-image').each do |scrape| 
     product2 = scrape['alt'] 

     scrapedProducts2 = "#{product2}"[0..-7] 
     scrapedURLs2 = "http://www.lyst.com#{link2}" 

     csv << [scrapedURLs2, scrapedProducts2] 
    end 
    end 
end 

## Here is where I am trying to combine the two columns into a new CSV. ## 
## It doesn't work. I suspect that this part should be further up... ## 

# CSV.open("productResults3.csv", "wb") do |csv| 
    # csv << [scrapedURLs1, scrapedProducts2] 
#end 
puts "upload complete!" 

感謝您的閱讀。

回答

0

謝謝你分享你的代碼和你的問題。我希望我的輸入有幫助!

  • scrapedURLs1 = "{link}"scrapedProducts1 = "#{scrape['alt']}"[0..-7]對年底,但你不csv << [scrapedProducts, scrapedURLs]稱之爲這是錯誤你得到

  • 我會建議你結合前兩步跳過 寫入文件,但跳轉到數組陣列,然後您可以將它們寫入 文件。

  • 你是否意識到,在示例代碼,你給 scrapedURLs1, scrapedProducts2將是一個錯誤的URL混合 錯誤的產品。那是你的意思嗎?

  • 在註釋掉的代碼scrapedURLs1, scrapedProducts2內不存在,它們沒有被聲明。您需要打開這兩個文件才能使用.each do |scrapedURLs1|讀取文件,然後使用.each do |scrapedProducts2|讀取另一個文件,然後這些變量將會存在,因爲each Enumerator會對它們進行實例化。

在內部迭代中重複使用相同的|scrape|變量不是一個好主意。將名稱更改爲其他名稱,例如|scrape2|。它「發生」工作,因爲在第二次循環之前,你已經在product=scrape['alt']中採取了你所需要的。如果您重命名第二個循環變量,您可以將product=scrape['alt']行移入內部循環併合並它們。例如:

# In your code example you may get many links per product. 
# If that was your intent then that may be fine. 
# This code should get one link per product. 
CSV.open("results1.csv", "wb") do |csv| 
    page.css('img.product-card-image').each do |scrape| 
    page.css('a.product-card-image-link').each do |scrape2| 
     #  [  product  ,  link  ] 
     csv << [scrape['alt'][0..-7], scrape2['href']] 
     # NOTE that scrape['alt'][0..-7] and scrape2['href'] are already strings 
     # so you don't need to use "#{ }" 
    end 
    end 
end 

邊注:如果您使用的CSV我強烈建議您使用詹姆斯·愛德華·格雷二世faster_csv寶石工作的Ruby 2.0.0不需要線require "rubygems"

。在這裏看到一個使用的例子:https://github.com/JEG2/faster_csv/blob/master/examples/csv_writing.rb

+0

感謝您回答丹,重申:您的觀點: – JammyStressford