Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby HTML类中有空格时如何选择元素_Ruby_Css Selectors_Web Scraping_Nokogiri - Fatal编程技术网

Ruby HTML类中有空格时如何选择元素

Ruby HTML类中有空格时如何选择元素,ruby,css-selectors,web-scraping,nokogiri,Ruby,Css Selectors,Web Scraping,Nokogiri,如何使用CSS选择器获取下面的“这是我需要的文本”行 我不知道如何处理table类中的空格 <table class="some name"> <thead> </thead> <tbody> <tr> <td style="text-align:center;">50</td> <td style="text-align:left;"><a href="/thing"

如何使用CSS选择器获取下面的“这是我需要的文本”行

我不知道如何处理table类中的空格

<table class="some name">
<thead> 
</thead>
<tbody>
    <tr>
    <td style="text-align:center;">50</td>
    <td style="text-align:left;"><a href="/thing" title="thing">This is the text I need</a></td>

50

如果class属性值中有空格,则表示有多个类应用于元素。要定位具有多个类的元素,css选择器只是一个类链。通常情况下,表单如下所示:

element.class1.class2
因此,假设该链接是具有类“some”和“name”的表中的第一个链接,则可以执行以下操作:

require 'nokogiri'

html = %Q{
<table class="some name">
    <thead> 
    </thead>
    <tbody>
        <tr>
            <td style="text-align:center;">50</td>
            <td style="text-align:left;"><a href="/thing" title="thing">This is the text I need</a></td>
        </tr>
    </tbody>
</table>
}

doc = Nokogiri::XML(html)

# Assuming you need both classes to uniquely identify the table
p doc.at_css('table.some.name a').text
#=> "This is the text I need"

# Note that you do not need to use both classes if one of them is unique
p doc.at_css('table.name a').text
#=> "This is the text I need"
需要“nokogiri”
html=%Q{
50
}
doc=Nokogiri::XML(html)
#假设您需要两个类来唯一标识表
p doc.at_css('table.some.name a')。文本
#=>“这是我需要的文本”
#请注意,如果其中一个类是唯一的,则不需要同时使用这两个类
p doc.at_css('table.name a')。文本
#=>“这是我需要的文本”