我完全被这个难住了。
我有以下代码:
puts block.at_xpath("*/img")["width"].to_i
但当我把它改成
width = block.at_xpath("*/img")["width"].to_i
我得到这个错误:
NokogiriTUT.rb:70:in `blockProcessor': undefined method `[]' for nil:NilClass (NoMethodError)
当我把看跌期权放进去时,它会返回期望值。
更新:
def blockProcessor(block)
header = block.xpath('td[@class="default"]/*/span[@class="comhead"]')
array = header.text.split
if array[0] != nil #checks to make sure we aren't at the top of the parent list
### Date and Time ###
if array[2] == 'hours' || array[2] == 'minutes'
date = Time.now
else
days = (array[1].to_i * 24 * 60 * 60)
date = Time.now - days
end
##Get Comment##
comment = block.at_xpath('*/span[@class="comment"]')
hash = comment.text.hash
#puts hash
##Manage Parent Here##
width = block.at_xpath("*/img")["width"].to_i
prevlevel = @parent_array[@parent_array.length-1][1]
if width == 0 #has parents
parentURL = header.xpath('a[@href][3]').to_s
parentURL = parentURL[17..23]
parentURL = "http://news.ycombinator.com/item?id=#{parentURL}"
parentdoc = Nokogiri::HTML(open(parentURL))
a = parentdoc.at_xpath("//html/body/center/table/tr[3]/td/table/tr")
nodeparent = blockProcessor(a)
@parent_array = []
node = [hash, width, nodeparent] #id, level, parent
@parent_array.push node
elsif width > prevlevel
nodeparent = @parent_array[@parent_array.length-1][0]
node = [hash, width, nodeparent]
@parent_array.push node
elsif width == prevlevel
nodeparent = @parent_array[@parent_array.length-1][2]
node = [hash, width, nodeparent]
@parent_array.push node
elsif width < prevlevel
until prevlevel == w do
@parent_array.pop
prevlevel = @parent_array[@parent_array.length-1][1]
end
nodeparent = @parent_array[@parent_array.length-1][2]
node = [hash, width, nodeparent]
@parent_array.push node
end
puts "Author: #{array[0]} with hash #{hash} with parent: #{nodeparent}"
##Handles Any Parents of Existing Comments ##
return hash
end
end
end
这是它所作用的积木。
<tr>
<td><img src="http://ycombinator.com/images/s.gif" height="1" width="0"></td>
<td valign="top"><center>
<a id="up_3004849" href="vote?for=3004849&dir=up&whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44"><img src="http://ycombinator.com/images/grayarrow.gif" border="0" vspace="3" hspace="2"></a><span id="down_3004849"></span>
</center></td>
<td class="default">
<div style="margin-top:2px; margin-bottom:-10px; "><span class="comhead"><a href="user?id=patio11">patio11</a> 12 days ago | <a href="item?id=3004849">link</a> | <a href="item?id=3004793">parent</a> | on: <a href="item?id=3004471">Ask HN: What % of your job interviewees pass FizzB...</a></span></div>
<br><span class="comment"><font color="#000000">Every time FizzBuzz problems come up among engineers, people race to solve them and post their answers, then compete to see who can write increasingly more nifty answers for a question which does not seek niftiness at all.<p>I'm all for intellectual gamesmanship, but these are our professional equivalent of a doctor being asked to identify the difference between blood and water. You can do it. <i>We know</i>. Demonstrating that you can do it is not the point of the exercise. We do it to have a cheap-to-administer test to exclude people-who-cannot-actually-program-despite-previous-job-titles from the expensive portions of the hiring process.</p></font></span><p><font size="1"><u><a href="reply?id=3004849&whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44">reply</a></u></font></p>
</td>
</tr>
最佳答案
您的基本问题是不理解XPath(在那里,你的同伴很好;XPath很混乱。)你的选择器根本不匹配你认为它们匹配的内容尤其是那个爆炸的
*/img
应该是
//img
或者类似的事情。
现在,因为xpath选择器与任何内容都不匹配,所以这个Ruby语句的值
block.at_xpath("*/img")
是零nil不支持
[]
,所以当您试图在它上调用["width"]
时,Ruby会抱怨一个undefined method [] for nil:NilClass
错误。至于为什么只有当你把它赋给一个变量时它才会爆炸…是的,事实并非如此。你可能也换了别的东西。
现在,请允许我提出一些其他有希望的建设性代码批评:
你的问题显然是为了让人难以回答以后,请把有问题的代码隔离出来,不要只粘贴在你的作业里(或者这个屏幕刮板是做什么用的)。
如果你把它做成一个可运行的Ruby文件,我们就可以在计算机上一字不差地执行它,例如:
是的。
require "nokogiri"
doc = Nokogiri.parse <<-HTML
<tr>
<td><img src="http://ycombinator.com/images/s.gif" height="1" width="0"></td>
<td valign="top"><center>
<a id="up_3004849" href="vote?for=3004849&dir=up&whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44"><img src="http://ycombinator.com/images/grayarrow.gif" border="0" vspace="3" hspace="2"></a><span id="down_3004849"></span>
</center></td>
<td class="default">
<div style="margin-top:2px; margin-bottom:-10px; ">
<span class="comhead">
<a href="user?id=patio11">patio11</a> 12 days ago | <a href="item?id=3004849">link</a> | <a href="item?id=3004793">parent</a> | on: <a href="item?id=3004471">Ask HN: What % of your job interviewees pass FizzB...</a>
</span>
</div>
<br><span class="comment"><font color="#000000">Every time FizzBuzz problems come up among engineers, people race to solve them and post their answers, then compete to see who can write increasingly more nifty answers for a question which does not seek niftiness at all.<p>I'm all for intellectual gamesmanship, but these are our professional equivalent of a doctor being asked to identify the difference between blood and water. You can do it. <i>We know</i>. Demonstrating that you can do it is not the point of the exercise. We do it to have a cheap-to-administer test to exclude people-who-cannot-actually-program-despite-previous-job-titles from the expensive portions of the hiring process.</p></font></span><p><font size="1"><u><a href="reply?id=3004849&whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44">reply</a></u></font></p>
</td>
</tr>
HTML
width = doc.at_xpath("*/img")["width"].to_i
这样我们就可以用计算机调试,而不仅仅是用头脑。
你现在写的是Ruby,而不是Java,所以要遵循Ruby的spacing and naming conventions:文件名是snake_case,缩进是2个空格,没有制表符等等。很难读取格式错误的代码——其中“错误”意味着“非标准”。
在任何地方,您都可以使用这些描述性注释(
### Date and Time ###
)来提取方法(def date_and_time(array)
)并使代码更干净、更易于调试。关于ruby - 无法将值赋给变量:nil:NilClass的未定义方法“[]”(NoMethodError),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7602252/