Cheerio does not like html without proper tags (who does, really?). I'm trying to scrape some menus and the content i want is between elements in the html. Is there a way to parse each of these and save them as chunks using cheerio? The menu is a 5 day menu with different courses below each day. The element (the days) is at the same level (siblings) to the menu items.
Here's the tricky part: The menu items does not have any attributes. It's a "raw" text node.
I'm not able to change the html.
Here is the html structure, note that the raw text nodes are siblings to the 'headers'.
<div class="meny" style="clear:left;line-height:1.6em;padding-bottom:2em;">
<strong>Måndag</strong>
<br>Klassisk wallenbergare på kalvfärs serveras med gräddsås, lingonsylt, och potatismos
<br>Dragonbrässerad fiskfilé serveras med basilika och ruccolacrème samt kokt potatis
<br>Pasta med strimlad ryggbiff, champinjoner och lök i krämig grönpepparsås
<br>Pasta vegetale med rostad paprika, lök och purjolök i krämig örtsås
<br>Grillad högrevsburgare serveras med ost, bacon, briochebröd och country fries
<br>
<br><strong>Tisdag</strong>
<br>Stekt fläsk med löksås, bruna bönor eller raggmunk och lingon
<br>Thailändsk biffgryta med citrongräs, kokosgrädde, limeblad, wokgrönsaker och rödcurry
<br>Hollandaisebakad torskfilé på purjolöksbädd serveras med vitvinssås och kokt potatis
<br>Pasta penne med bacon, ädelost, champinjoner, blomkål och grädde
<br>Vegetarisk Thaigryta serveras med jasminris
<br>Grillad högrevsburgare serveras med ost, bacon, briochebröd och country fries
<br>
<br><strong>Onsdag</strong>
<br>Kycklingschnitzel serveras med barbequesås och rostad kulpotatis
<br>Honung och enbärsbakad laxfilé serveras med citruscrème och örtslungad potatis
<br>Pasta med strimlad kycklingfilé i rosmarin och citronsås
<br>Karibisk falafelrulle med salsa och vitlöksdressing
<br>Grillad högrevsburgare serveras med ost, bacon, briochebröd och country fries
<br>
<br><strong>Torsdag</strong>
<br>Grillad karréskiva serveras med bearnaisesås och klyftpotatis
<br>Stekt dubbelpanerad fiskfilé serveras med dill och rödlöksröra
<br>Pasta med strimlad fläskfilé, paprika och lök i krämig gorgonzolasås
<br>Grillad högrevsburgare serveras med ost, bacon, briochebröd och country fries
<br>
<br><strong>Fredag</strong>
<br>LÅNGFREDAG STÄNGT</div>
desired json output, the "day" is for i18n...
weekMenu = {
name: "menuname",
weekDayMenus: {
monday: {
day: "",
dayMenu: "",
},
tuesday: {
day: "",
dayMenu: "",
},
wednesday: {
day: "",
dayMenu: "",
},
thursday: {
day: "",
dayMenu: "",
},
friday: {
day: "",
dayMenu: "",
},
}
};
Here's what i tried so far, needless to say, it does not work since it only outputs the headers. It doesn't include the text-node children with contents() since they're siblings and not children.
cheeriojs
var a = $('div.meny')
.clone()
.remove().eq(0) //remove second menu div
.find('strong').eq(0) //point to first 'day'
.siblings() //select the other days
.remove() //remove the other days
.end() //move pointer back to the start
.contents() //get the text nodes associated with the selected 'header'
.filter(function (idx, elem) {
console.log(elem.data)
return elem.type === 'text';
})
.end()
.text()
and not one coherent text node. – herr_hest Apr 22 '19 at 11:40