Ruby脚本从Mechanize 2.1过渡到1.0 - 选择下拉框

时间:2012-02-03 13:00:35

标签: html ruby mechanize

由于使用Mechanize 2.1的Ruby脚本遇到了一些代理问题,我想将脚本转换为使用Mechanize 1.0。我已经取得了一些进展,但现在我被困住了。

该脚本会抓取亚马逊页面以列出并在给定的时间跨度内乘以所有订单。使用“登录”表单登录后,我们前往“您的帐户” - >你的订单“现在尝试抓住这个下拉框: dropdown box

这是来自亚马逊的HTML代码:

<form id="order-dropdown-form" action="/gp/css/order-history" method="get">
    <input type="hidden" name="opt" value="ab">
    <label for="orderFilter">
        Date:
    </label>
    <select name="orderFilter" id="orderFilter">
        <option value="select-another" disabled="disabled">
            -Select different orders to view-
        </option>
        <option value="last30" selected="selected">
            Orders placed in the last 30 days
        </option>
        <option value="months-6">
            Orders placed in the past 6 months
        </option>
        <option value="year-2012">
            Orders placed in 2012
        </option>
        <option value="year-2011">
            Orders placed in 2011
        </option>
        (...)
    </select>
   <span class="in-amzn-btn btn-prim-med" unselectable="on"><input type="submit" value="Go"><span></span></span>
</form>

ruby​​代码如下所示:

select_form = orders_page.form_with(:id => 'order-dropdown-form')
select_form.field_with(:name => 'orderFilter').options.each do |option|

不幸的是,这不适用于Mechanize 1.0,没有方法“id”。那我怎么能得到那个下拉框呢?

1 个答案:

答案 0 :(得分:0)

不清楚你需要做什么。如果你需要设置它的值:

select_form['orderFilter'] = value

如果你需要遍历选项,它更像是:

orders_page.search('select#orderFilter option').each do |option|
   select_form['orderFilter'] = option[:value]
end

请注意,这样做意味着选项是Nokogiri :: XML :: Node而不是Mechanize :: Form :: Option或其他。我这样做似乎更容易。