gpt4 book ai didi

ruby - 如何从字符串中使用 Mechanize 或 Nokogiri 解析表单

转载 作者:行者123 更新时间:2023-12-04 16:18:37 24 4
gpt4 key购买 nike

我需要解析表单以从我返回的 HTML 中获取`IW_SessionID_的值,我无法开始工作。

#!/usr/bin/ruby

require 'pp'
require 'nokogiri'
require 'mechanize'

r = '<HTML><HEAD><TITLE></TITLE><meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n<NOSCRIPT><HTML><BODY>Your browser does not seem to support JavaScript. Please make sure it is supported and activated</BODY></HTML></NOSCRIPT>\r\n<SCRIPT>\r\nvar ie4 = (document.all)? true:false;\r\nvar ns6 = (document.getElementById)? true && !ie4:false;\r\nfunction Initialize() {\r\nvar lWidth;\r\nvar lHeight;\r\nif (ns6) {\r\n lWidth = window.innerWidth - 30;\r\n lHeight = window.innerHeight - 30;\r\n} else {\r\n lWidth = document.body.clientWidth;\r\n lHeight = document.body.clientHeight;\r\n if (lWidth == 0) { lWidth = undefined;}\r\n if (lHeight == 0) { lHeight = undefined;}\r\n}\r\ndocument.forms[0].elements[\"IW_width\"].value = lWidth;\r\ndocument.forms[0].elements[\"IW_height\"].value = lHeight;\r\ndocument.forms[0].submit();\r\n}</SCRIPT></HEAD><BODY onload=\"Initialize()\">\r\n<form method=post action=\"/bwtem\">\r\n<input type=hidden name=\"IW_width\">\r\n<input type=hidden name=\"IW_height\">\r\n<input type=hidden name=\"IW_SessionID_\" value=\"1wqzj1f0vec57r1apfqg51wzs88c\">\r\n<input type=hidden name=\"IW_TrackID_\" value=\"0\">\r\n</form></BODY></HTML>'

page = Nokogiri::HTML r
puts page.css('form[name="IW_SessionID_"]')

a = Mechanize.new
page2 = Mechanize::Page.new(nil,{'content-type'=>'text/html'},r,nil,a)

pp page2.form_with(:name => "IW_SessionID_")

脚本只返回 nil .

谁能弄清楚如何获得 IW_SessionID_ 的值? ?

最佳答案

您必须取消转义示例 HTML 字符串,然后搜索 输入 与名称 IW_SessionID_ .

此示例代码适用于我:

#!/usr/bin/ruby

require 'pp'
require 'nokogiri'
require 'mechanize'

r = '<HTML><HEAD><TITLE></TITLE><meta http-equiv="cache-control" content="no-cache">\r\n<meta http-equiv="pragma" content="no-cache">\r\n<NOSCRIPT><HTML><BODY>Your browser does not seem to support JavaScript. Please make sure it is supported and activated</BODY></HTML></NOSCRIPT>\r\n<SCRIPT>\r\nvar ie4 = (document.all)? true:false;\r\nvar ns6 = (document.getElementById)? true && !ie4:false;\r\nfunction Initialize() {\r\nvar lWidth;\r\nvar lHeight;\r\nif (ns6) {\r\n lWidth = window.innerWidth - 30;\r\n lHeight = window.innerHeight - 30;\r\n} else {\r\n lWidth = document.body.clientWidth;\r\n lHeight = document.body.clientHeight;\r\n if (lWidth == 0) { lWidth = undefined;}\r\n if (lHeight == 0) { lHeight = undefined;}\r\n}\r\ndocument.forms[0].elements["IW_width"].value = lWidth;\r\ndocument.forms[0].elements["IW_height"].value = lHeight;\r\ndocument.forms[0].submit();\r\n}</SCRIPT></HEAD><BODY onload="Initialize()">\r\n<form method=post action="/bwtem">\r\n<input type=hidden name="IW_width">\r\n<input type=hidden name="IW_height">\r\n<input type=hidden name="IW_SessionID_" value="1wqzj1f0vec57r1apfqg51wzs88c">\r\n<input type=hidden name="IW_TrackID_" value="0">\r\n</form></BODY></HTML>'

page = Nokogiri::HTML r
input = page.css('input[name="IW_SessionID_"]').first
puts input[:value]

关于ruby - 如何从字符串中使用 Mechanize 或 Nokogiri 解析表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43364048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com