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BQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0IAgAAAAgIAAAAAAoGAwAAACBmNjZlZTgwODEwZWM0ZjcwYThhZjY2ZDcyNDlmNWFjZgEAAAALZGrI7rw4FqPtCexAP1+dCQ7Qps1t">

我尝试使用这样的正则表达式:

VIEWSTATE = (/<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" \/>/.match body_text)[1]

但似乎,它的工作方式很奇怪,并没有获取所有价值,而只是其中的一部分。在那种情况下,我必须使用哪个正则表达式?(请注意,正则表达式中需要 VIEWSTATE" id="_VIEWSTATE" value= 部分)。

会很好,如果你告诉我怎么做)

4

1 回答 1

2
require 'nokogiri'

doc = Nokogiri::HTML::Document.parse <<-eot
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTIwMjYxMTgwOTAPFgIeB">
eot

# using css
nd = doc.at_css('input#__VIEWSTATE')
nd['name'] 
# => "__VIEWSTATE"

# using xpath
nd = doc.at('//div[@id ="__VIEWSTATE"]')
nd['name'] 
# => "__VIEWSTATE"

This way you can get every attribute value of the input html element.

于 2013-09-19T12:56:42.870 回答