如果得到元素<section>
:
client = Mechanize.new
body = client.get("https://www.linternaute.fr/dictionnaire/fr/definition/perdu/")
section = body.search('.dico_definition')
并且我想获得所有出现的<div class="dico_title_2">
仅在此<section>
中的内容
我尝试过:
[section.search('.dico_title_2')
,但它获得所有页面的<div class="dico_title_2">
,而不仅是<section>
中的html内容。
[section.at('.dico_title_2')
获得<div class="dico_title_2">
中的<section>
,但仅是第一次出现...
如何获得所有事件?
我发现可能是问题所在:
JavaScript:document.querySelectorAll('section.dico_definition > *')
返回7个元素,但data.search('section.dico_definition')
返回4个元素...
也许在解释html时出现问题?
在这种情况下我该怎么办?
尝试一下:
client = Mechanize.new
data = client.get("https://www.linternaute.fr/dictionnaire/fr/definition/perdu/")
section = data.search('section//.dico_title_2')
section.first # This is the first element
^^^这将找到嵌套在节标记中的.dico_title_2
类的所有标记。
注意:变量名body
可能与标记内容混淆,因此我将其命名为html_content
或data
。