使用ruby在抓取过程中提交登录字段?

时间:2014-08-17 01:54:55

标签: ruby web-scraping nokogiri mechanize typhoeus

我需要从名为NetTeller的系统中提取一些财务数据。

可以找到一个示例here

请注意初始ID字段提示:

ID field

然后,一旦提交,您必须输入密码: Password field

如您所见,它有两个步骤,您首先输入一个ID号,然后在提交后向用户显示一个密码字段。在进入系统并获取我真正想要的数据之前,我在这里遇到了一些道路颠簸。如何处理这样的场景,您需要先获取身份验证字段,然后才能获取要扫描的数据?

我假设我可以使用httpclientnokogiri进入,但我很好奇是否在进入目标之前处理这样的两页登录时有任何技巧。

1 个答案:

答案 0 :(得分:1)

我会使用Mechanize。第一页是'#34;棘手"因为登录表单在iframe中。因此,您只能使用加载iframe的源代码。方法如下:

agent = Mechanize.new

# Get first page
iframe_url = 'https://www.banksafe.com/sfonline/'
page = agent.get(iframe_url)
login_form = page.forms.first
username_field = login_form.field_with(:name => "12345678")

# Get second page
response = login_form.submit
second_login_form = response.forms.first
password_field = second_login_form.field_with(:password => "xxxxx")

# Get page to scrap
response = second_login_form.submit

这就是你如何处理这样的场景。显然,您可能需要完全适应这些表单/字段的编写方式以及其他特定页面的详细信息,但我会采用这种方法。