Skip to content
Advertisement

Get all the texts in h2 and return an array readable (via APIFY)

I’m using “Web scraper apify” to scrap some data from a website. The goal is to get all the texts in H2 and return an array of them. My problem is when I returned the array. This one is not correct and not usable because it separates all the letters of the different scrapped texts.

I tried to write this code (javascript and jquery including):

JavaScript
JavaScript

And I have this result when I export in JSON

JavaScript

I would like something like

JavaScript

Advertisement

Answer

Regardless of the cause of the string being split to a character array, to get the desired output, change the map so that it returns the object that you want, rather than just the text:

JavaScript

gives you an array of the text, but you want an array of { tool: <text> }, so:

JavaScript

(note: jquery .map() requires .get() at the end to convert to a “true” array)

You’ll then have an array of objects, which you can json stringify to get your desired JSON output:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement