0

知道如何将正确的参数传递给 xpath 吗?必须有一些关于如何使用单引号/双引号的东西。当我使用变量时

parser_xpath_identificator = "'//table/tbody[@id=\"threadbits_forum_251\"]/tr'"给我一个不正确的值或

parser_xpath_identificator = "'//table/tbody[@id="threadbits_forum_251"]/tr'"给我一个错误语法错误,意外的 tIDENTIFIER,期待 $end

require 'rubygems'
require 'mechanize'

parser_xpath_identificator = "'//table/tbody[@id=\"threadbits_forum_251\"]/tr'"
#   parser_xpath_identificator = "'//table/tbody[@id="threadbits_forum_251"]/tr'"
    #gives an error: syntax error, unexpected tIDENTIFIER, expecting $end

agent = WWW::Mechanize.new
page = agent.get("http://www.vbulletin.org/forum/index.php")
page = page.link_with(:text=>'vB4 General Discussions').click
puts "Page title: #{page.title}"
puts "\nfrom variable: #{page.parser.xpath(parser_xpath_identificator).length}"
puts "directly: #{page.parser.xpath('//table/tbody[@id="threadbits_forum_251"]/tr').length}"
4

1 回答 1

2

在这两种情况下,您都将单引号直接嵌套在双引号内,我认为这是不正确的。试试这个:

parser_xpath_identificator = '//table/tbody[@id="threadbits_forum_251"]/tr'
于 2010-01-27T03:12:25.807 回答