我正在尝试从dictionary.com写一个scrape函数,我试图得到这个词和它的发音。 我有这个div,我需要从中获取数据,但我无法,不知道我做错了什么。我正在获取其他div的数据(这是一个单词)就好了但这个特殊的div(发音)......没有。 jquery的新手,node.js.任何朝着正确方向的事情都会很棒。
这是我尝试过的其他div就好了。(从div中获取'word'工作正常但是当我来发音时它不起作用)
$('.header-row-header-extras-pronounce-pronset').filter(function() {
var data = $(this);
pronunciation += data.children().first().text();
console.log("prp: " + pronunciation);
json.pronunciation = pronunciation;
})
<div class="header-row header-extras pronounce pronset">
<div>
<span class="pron spellpron" style="display: inline;">[<span class="dbox-bold">ak</span>-tiv]</span>
<span class="pron ipapron" style="display: none;">/ˈæk tɪv/ </span>
<span class="pre-def-data"> </span>
</div>
<div>
<button class="prontoggle pronounce-button pronounce-Spell-button">Spell</button>
<button class="syllable-button syllable-button-off" data-header="1">Syllables</button>
</div>
</div>
答案 0 :(得分:1)
三件事:
此:
$('.header-row-header-extras-pronounce-pronset')
使用单一类 header-row-header-extras-pronounce-pronset
查找元素。但是你的div没有一个具有该名称的类,它有多个类,其中包含该名称的各个部分。要在元素上搜索多个类,可以使用多个类选择器:
$(".header-row.header-extras.pronounce.pronset")
// Note ------^-------------^---------^
filter
用于创建一组元素的子集。如果您未使用其返回值,请改用each
。
json.pronunciation = pronunciation;
将覆盖 json.pronunciation
,因此您最终只能使用最后一个回调。如果没有更多背景信息,我们无法告诉您如何解决这个问题,但我认为我应该将其标记出来。
答案 1 :(得分:1)
您的选择器正在选择一个长类,其中标记具有单独的类。
加价:
<div class="header-row header-extras pronounce pronset">
使用此选择器:
$('.header-row.header-extras.pronounce.pronset').
选择jQuery中的元素就像使用CSS选择它们一样。
答案 2 :(得分:0)
var pronunciation = [];
$('.spellpron .dbox-bold').each(function(){
pronunciation.push($(this).text());
});
console.log(pronunciation);
<div class="header-row header-extras pronounce pronset">
<div>
<span class="pron spellpron" style="display: inline;">[<span class="dbox-bold">ak</span>-tiv] </span>
<span class="pron ipapron" style="display: none;">/ˈæk tɪv/ </span>
<span class="pre-def-data"> </span>
</div>
<div>
<button class="prontoggle pronounce-button pronounce-Spell-button">Spell</button>
<button class="syllable-button syllable-button-off" data-header="1">Syllables</button>
</div>
</div>
<div class="header-row header-extras pronounce pronset">
<div>
<span class="pron spellpron" style="display: inline;">[<span class="dbox-bold">ak</span>-tiv] </span>
<span class="pron ipapron" style="display: none;">/ˈæk tɪv/ </span>
<span class="pre-def-data"> </span>
</div>
<div>
<button class="prontoggle pronounce-button pronounce-Spell-button">Spell</button>
<button class="syllable-button syllable-button-off" data-header="1">Syllables</button>
</div>
</div>
<div class="header-row header-extras pronounce pronset">
<div>
<span class="pron spellpron" style="display: inline;">[<span class="dbox-bold">ak</span>-tiv] </span>
<span class="pron ipapron" style="display: none;">/ˈæk tɪv/ </span>
<span class="pre-def-data"> </span>
</div>
<div>
<button class="prontoggle pronounce-button pronounce-Spell-button">Spell</button>
<button class="syllable-button syllable-button-off" data-header="1">Syllables</button>
</div>
</div>
<div class="header-row header-extras pronounce pronset">
<div>
<span class="pron spellpron" style="display: inline;">[<span class="dbox-bold">ak</span>-tiv] </span>
<span class="pron ipapron" style="display: none;">/ˈæk tɪv/ </span>
<span class="pre-def-data"> </span>
</div>
<div>
<button class="prontoggle pronounce-button pronounce-Spell-button">Spell</button>
<button class="syllable-button syllable-button-off" data-header="1">Syllables</button>
</div>
</div>
<div class="header-row header-extras pronounce pronset">
<div>
<span class="pron spellpron" style="display: inline;">[<span class="dbox-bold">ak</span>-tiv] </span>
<span class="pron ipapron" style="display: none;">/ˈæk tɪv/ </span>
<span class="pre-def-data"> </span>
</div>
<div>
<button class="prontoggle pronounce-button pronounce-Spell-button">Spell</button>
<button class="syllable-button syllable-button-off" data-header="1">Syllables</button>
</div>
</div>
<div class="header-row header-extras pronounce pronset">
<div>
<span class="pron spellpron" style="display: inline;">[<span class="dbox-bold">ak</span>-tiv] </span>
<span class="pron ipapron" style="display: none;">/ˈæk tɪv/ </span>
<span class="pre-def-data"> </span>
</div>
<div>
<button class="prontoggle pronounce-button pronounce-Spell-button">Spell</button>
<button class="syllable-button syllable-button-off" data-header="1">Syllables</button>
</div>
</div>