Ruby 使用Mechanize Nokogiri Selenium在网站上发布数据
我需要通过一个程序在网站上发布数据。 为了实现这一点,我正在使用Mechanize Nokogiri和Selenium。 这是我的密码:Ruby 使用Mechanize Nokogiri Selenium在网站上发布数据,ruby,selenium,web-scraping,nokogiri,mechanize,Ruby,Selenium,Web Scraping,Nokogiri,Mechanize,我需要通过一个程序在网站上发布数据。 为了实现这一点,我正在使用Mechanize Nokogiri和Selenium。 这是我的密码: def aeiexport # first Mechanize is submitting the form to identify yourself on the website agent = Mechanize.new agent.get("https://www.glou.com")
def aeiexport
# first Mechanize is submitting the form to identify yourself on the website
agent = Mechanize.new
agent.get("https://www.glou.com")
form_login_AEI = agent.page.forms.first
form_login_AEI.util_vlogin = "42"
form_login_AEI.util_vpassword = "666"
# this is suppose to submit the form I think
page_compet_list = agent.submit(form_login_AEI, form_login_AEI.buttons.first)
#to be able to scrap the page you end up on after submitting form
body = page_compet_list.body
html_body = Nokogiri::HTML(body)
#tds give back an array of td
tds = html_body.css('.L1').xpath("//table/tbody/tr[position()>1]/td")
# Checking my array of td with some condition
tds.each do |td|
link = td.children.first # Select the first children
if link.html = "2015 32 92 0076 012"
# Only consider the html part of the link, if matched follow the previous link
previous_td = td.previous
previous_url = previous_td.children.first.href
#following the link contained in previous_url
page_selected_compet = agent.get(previous_url)
# to be able to scrap the page I end up on
body = page_selected_compet.body
html_body = Nokogiri::HTML(body)
joueur_access = html_body.search('#tabs0head2 a')
# clicking on the link
joueur_access.click
rechercher_par_numéro_de_licence = html_body.css('.L1').xpath("//table/tbody/tr/td[1]/a[1]")
pure_link_rechercher_par_numéro_de_licence = rechercher_par_numéro_de_licence['href']
#following pure_link_rechercher_par_numéro_de_licence
page_submit_licence = agent.get(pure_link_rechercher_par_numéro_de_licence)
body_submit_licence = page_submit_licence.body
html_body = Nokogiri::HTML(body_submit_licence)
#posting my data in the right field
form.field_with(:name => 'lic_cno[0]') == "9511681"
1) 到目前为止,您对这段代码有什么看法,您认为其中有错误吗
2) 这部分我真的不确定:我已经在正确的字段中发布了我的数据,但现在我需要提交它。问题是我需要单击的按钮如下所示:
<input type="button" class="button" onclick="dispatchAndSubmit(document.JoueurRechercheForm, 'rechercher');" value="Rechercher">
但到目前为止,我还没有成功地发布数据。
你能告诉我硒是否能让我做我需要做的事吗。如果可以的话?乍一看,您的代码可以使用更少的缩进和更多的空格/空行来分隔
AEIexport
的内部逻辑(应该更改为aei\u export
,因为Ruby使用snake case作为方法名。您可以找到更多关于如何设置Ruby代码样式的建议)
除了代码的样式之外,我在方法开头发现的一个错误是在定义form\u login\u AEI
时使用了未定义的变量page
关于你的第二个问题,我不熟悉硒;但是,由于它使用的是真正的web浏览器,所以它可以处理JavaScript。这是另一种可能的解决办法
另一种方法是查看页面源代码(即在Firebug中),并了解页面上的JavaScript的功能。然后使用Mechanize手动跟踪链接
driver.find_element(:value=> 'Rechercher').click
driver.find_element(:name=> 'sel').click
driver.find_element(:value=> 'Sélectionner').click
driver.find_element(:value=> 'Inscrire').click