如何写方法到csv不只是字符串? Ruby CSV宝石 [英] How to write method to csv not just string? Ruby csv gem

查看:81
本文介绍了如何写方法到csv不只是字符串? Ruby CSV宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文本内容从html元素放到csv文件中.对于ruby csv gem,即使指定了对象,用于包装的String和IO的主要写入方法似乎也只能转换字符串.

I need to put the text content from an html element to a csv file. With the ruby csv gem, it seems that the primary write method for wrapped Strings and IOs only converts a string even if an object is specified.

例如:

 Searchresults = puts browser.divs(class: 'results_row').map(&:text)
 csv << %w(Searchresults)

仅返回csv文件中的"searchresults".

returns only "searchresults" in the csv file.

似乎应该有一种方法可以从div元素中指定要放置的文本,而不仅仅是文字字符串.

It seems like there should be a way to specify the text from the div element to be put and not just a literal string.

好的arieljuod和spickermann是正确的.现在,我将文本内容从div元素输出到csv,但并非像输出到控制台时那样全部. div元素"results_row"有两个具有文本内容的元素.它还有一个子div"results_subrow",其中一段文本内容未写入CSV. HTML:

Okay arieljuod and spickermann were right. Now I am getting text content from the div element output to the csv, but not all of it like when I output to the console. The div element "results_row" has two a elements with text content. It also has a child div "results_subrow" with a paragraph of text content that is not getting written to the csv. HTML:

<div class="bodytag" style="padding-bottom:30px; overflow:visible"> 
<h2>Search Results for "serialnum3"</h2>
<div id="results_banner">
    Products
    <span>Showing 1 to 2 of 2 results</span>
</div>
<div class="pg_dir"></div>
<div class="results_row">
    <a href="/products/fuji/" title="Fuji, Inc.">FUJI</a>
    <a href="/products/fuji/50mm lens/" title="Fuji, Inc.">50mm lens</a>
    <div class="results_subrow">
        <p>more product info</p>
    </div>
</div>
<div class="results_row">
    <a href="/products/fuji/" title="Fuji, Inc. 2">FUJI</a>
    <a href="/products/fuji/50mm lens/" title="Fuji, Inc.">50mm lens</a>
    <div class="results_subrow">
        <p>more product info 2</p>  
    </div>
</div>
<div class="pg_dir"></div>  

我的代码:

 search_results = browser.divs(class: 'results_row').map(&:text)
 csv << search_results

我认为在定位器中包含子div"results_subrow"会找到我所缺少的内容.喜欢:

I'm thinking that including the child div "results_subrow" in the locator will find what I am missing. Like:

search_results = browser.divs(class: 'results_row', 'results_subrow').map(&:text)
 csv << search_results

推荐答案

%w[Searchresults]创建一个包含单词Searchresults的数组.您可能想要这样的东西:

%w[Searchresults] creates an array containing the word Searchresults. You probably want something like this:

# assign the array returned from `map` to the `search_results` variable
search_results = browser.divs(class: 'results_row').map(&:text)

# output the `search_results`. Note that the return value of `puts` is `nil`
# therefore something like `Searchresults = puts browser...` doesn't work
puts search_results

# append `search_results` to your csv 
csv << search_results

这篇关于如何写方法到csv不只是字符串? Ruby CSV宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆