【问题标题】:How to use Mechanize to click on a link to scrape more details of an order?如何使用 Mechanize 点击链接来抓取订单的更多详细信息?
【发布时间】:2013-12-09 21:10:32
【问题描述】:

在订单索引页面上,我正在遍历所有 singleOrder (http://pastebin.com/FtiTBXG4)。我想要做的是单击每个订单链接,以便能够解析有关订单的更多信息。

但点击或迭代似乎不起作用。它只是不断点击第一个订单。

这是我正在使用的代码:

require 'mechanize'

a = Mechanize.new

a.get('http://exampleshop.nl/admin/') do |page|

    # Select the login form
    login_form = page.forms.first

    # Insert the username and password
    login_form.username = 'username'
    login_form.password = 'password'

    # Submit the login information
    dashboard_page = a.submit(login_form, login_form.buttons.first)

    # Check if the login was successfull
    puts check_1 = dashboard_page.title == 'Dashboard' ?  "CHECK 1 DASHBOARD SUCCESS" : "CHECK 1 DASHBOARD FAIL"

    # Visit the orders index page to scrape some standard information
    orders_page = a.click(dashboard_page.link_with(:text => /Bestellingen/))

    # pp orders_page # => http://pastebin.com/L3zASer6

    # Check if the visit is successful
    puts check_2 = orders_page.title == 'Bestellingen' ?  "CHECK 2 ORDERS SUCCESS" : "CHECK 2 ORDERS FAIL"

    # Search for all #singleOrder table row's and put them in variable all_single_orders
    all_single_orders = orders_page.search("#singleOrder") 

    # puts all_single_orders.class  # => Nokogiri::XML::NodeSet
    # puts all_single_orders                # => http://pastebin.com/FtiTBXG4
    # pp all_single_orders                  # => http://pastebin.com/UMRxGDn2

    # Scrape the needed information (the actual save to database is omitted)
    all_single_orders.each do |order|
        # Fetch the standard information
        puts orderId = order.search("#orderId").text                # => 259    
        puts customerName = order.search("#customerName").text      # => Firstname Lastname     
        puts orderStatus = order.search("#orderStatus").text        # => Bestelling ontvangen           
        puts orderAmount = order.search("#orderAmount").text            # => € 41,94

        # pp order # => sample of a a single `order` iteration: http://pastebin.com/FkM8DVT8

        # Visit a single order page to fetch more detailed information
        single_order_page = orders_page.link_with(:text => /Bekijk/).click

        # puts single_order_page.class # => Mechanize::Page

        # Print the URI to check what page we're on
        puts single_order_page.uri # => http://www.fonexshop.nl/admin/index.php?route=sale/order/info&token=SOMETOKEN&order_id=259
    end
end

这是输出:

CHECK 1 DASHBOARD SUCCESS
CHECK 2 ORDERS SUCCESS
http://www.exampleshop.nl/admin/index.php?route=sale/order/info&token=e29984974b56db4ba9d3c91a47d26f90&order_id=259
http://www.exampleshop.nl/admin/index.php?route=sale/order/info&token=e29984974b56db4ba9d3c91a47d26f90&order_id=259
http://www.exampleshop.nl/admin/index.php?route=sale/order/info&token=e29984974b56db4ba9d3c91a47d26f90&order_id=259
...

关于如何解决这个问题的任何想法?我使用 Ruby 2.0.0 和 Mechanize 2.7.3。

【问题讨论】:

    标签: ruby nokogiri mechanize


    【解决方案1】:

    问题出在这条线:

    single_order_page = orders_page.link_with(:text => /Bekijk/).click
    

    在这里,您告诉 Mechanize 单击页面上带有“Bekijk”文本的第一个链接。请注意,这是在页面上寻找第一个匹配的链接,而不仅仅是在订单中(即表格行)。

    我认为您需要按顺序获取链接的href,然后单击带有该href的链接(或直接导航到该链接):

    all_single_orders.each do |order|
        # Fetch the standard information
        puts orderLink = order.at_css("a")['href']  #Assuming first link in row
    
        # Visit a single order page to fetch more detailed information
        single_order_page = orders_page.link_with(:href => orderLink).click
    
        # Print the URI to check what page we're on
        puts single_order_page.uri # => http://www.fonexshop.nl/admin/index.php?route=sale/order/info&token=SOMETOKEN&order_id=259
    end
    

    【讨论】:

    • 我收到了一个TypeError,其中包含以下消息in []': no implicit conversion of String into Integer (TypeError)。我已经用我所做的和结果更新了原始问题。有什么想法吗?
    • 抱歉,search 返回的是节点集而不是元素。将search 更改为at_css
    猜你喜欢
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多