Skip to content
Advertisement

Puppeteer & cycling a process through multiple users

I’m trying to scrape information from a webpage behind a login wall for two users. As it stands, I’ve managed to get the code to do what I want for the first user i.e. go to webpage, login, gather the links associated with properties in a saved list, use that list to gather more details and log them to console.

The challenge I have now is getting the code to loop this round the second user without having to dupe the code. How would you suggest I go about it?

Secondly I need to make the array for each user, declared as uniquePropertyLinks in the below, accessible outside of the function userProcess.

How can I produce a new array for each user?

How can I access the array outside the function?

Here is the code:

JavaScript

Advertisement

Answer

Let’s see some of the things you might need to complete your task. I think it’s better to take time and develop the skills yourself, but I can perhaps point out a few key things.

You use:

JavaScript

but then you’re talking about looping. With such a data structure, it will be difficult to loop these two users. I recommend putting it into an object like so:

JavaScript

then you can easily look with for example for .. in:

JavaScript

or with .forEach():

JavaScript

need to make the array for each user, declared as uniquePropertyLinks in the below, accessible outside of the function userProcess.

Then declare the array outside of the funtion:

JavaScript

How can I produce a new array for each user? How can I access the array outside the function?

Again, it’d be better to choose a differen data structure, let’s day an object with keys that would represent each user and values would be arrays. It’d look like so:

JavaScript

which looks like this:

JavaScript

so you can save whatever values for user a into uniquePropertyLinks.a array and whatever values you need into uniquePropertyLinks.b array:

JavaScript

similarly for user b.

Now you should have all the bits you need in order to go back to your code and make the necessary changes.

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