Ruby,nokogiri和csv:关于不同的数据集

时间:2013-04-05 15:54:11

标签: ruby arrays parsing csv nokogiri

我有这样的事情:

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

fields = ['Full name', 'Address', 'Valid phone', 'E-mail']
availableFields = []
availableDataFields = []

CSV.open('myfile4.csv', 'a', {:col_sep => ';', :force_quotes => true}) do |csv|
    doc = Nokogiri::HTML(open(some_of_my_urls))

    #We are on the right page if we have content in this tag
    if doc.xpath('//div[@class = "pad8"]/h3').length > 0

        availableFields << 'Full name'
        availableFields << doc.xpath('//div[@class = "pad8"]/div[@class = "block pad8"]/table/tr/td[1]').text.gsub(/\n+|\r/, "").strip.split(':')
        #We received and parsed available fields on this page from td[1]
        #availableFields = ['Full name', 'Address', 'E-mail']

        availableDataFields << doc.xpath('//div[@class = "pad8"]/h3').text
        availableDataFields << doc.xpath('//div[@class = "pad8"]/div[@class = "block pad8"]/table/tr/td[3]').text.gsub(/\n+|\r/, "").strip
        #We received and parsed available fields data on this page from another td (td[3])
        #availableDataFields = ['John Doe', 'New York', 'john.doe@mail.com']

    end

    #Write to CSV file
    csv << fields
    csv << availableDataFields

end

所以当我打开CSV文件时,我有这个:

------------------------------------------------------------------------------------------------------
|     Full name    |    Address   |         Valid phone       |              E-mail                  |
| ---------------------------------------------------------------------------------------------------|
|     John Doe     |   New York   |      john.doe@mail.com    |                                      |
------------------------------------------------------------------------------------------------------

而不是

------------------------------------------------------------------------------------------------------
|     Full name    |    Address   |         Valid phone       |               E-mail                 |
| ---------------------------------------------------------------------------------------------------|
|     John Doe     |   New York   |                           |          john.doe@mail.com           |
------------------------------------------------------------------------------------------------------

由于我的某些网页(some_of_my_urls)可能包含的内容不完整,但我们可以解析其中哪些网页有,我们肯定知道该网页可能包含的所有可用字段(fields)。

我如何正确解决这个问题?我如何(可能)使用availableFields来解决这个问题。也许比较fieldsavailableFields并基于此生成正确的csv数据行?但是如何?

编辑:以下是HTML代码:case 1case 2

0 个答案:

没有答案