How to implement persistent login with crawlee-js/playwright?
I need to scrape content on multiple pages in one social network (x.com) that requires auth. Where to implement the login mechanism in order to it happened before following urls and persisted to use it until it is valid?
8 Replies
Someone will reply to you shortly. In the meantime, this might help:
flat-fuchsia•5mo ago
await page.context().storageState({ path: authFilePath })
Look up storageState() on the playwright docssensitive-blue•4mo ago
that gets the cookie and localstorage state but how do you load it into a new session. im facing the same dilema
flat-fuchsia•4mo ago
You can store you cookies in named KV store and then modify Your session with function:
https://crawlee.dev/api/next/core/interface/SessionPoolOptions#createSessionFunction
or You can do the same (update your requests with those cookies) in preNavigationHooks (https://crawlee.dev/api/next/browser-crawler/interface/BrowserCrawlerOptions#preNavigationHooks)
sensitive-blue•4mo ago
what about local storage? im supprised there seems to be no easy way to seed a session with local data
and also, setCookie wants a raw cookie string and a url rather than the format the getState() gives you
@osenvosem I have found a solution though it isnt great. If when creating your scraper, you set
useIncognitoPages
to true, you can modify the pageOptions to set your cookies and localStorage inside of a prePageCreateHook
unfortunately this seems to add a fair chunk of overhead though.
crawl with incognito
INFO PlaywrightCrawler: Final request statistics:
without incognito (and the hook)
INFO PlaywrightCrawler: Final request statistics:
@Crafty just advanced to level 4! Thanks for your contributions! 🎉
sensitive-blue•4mo ago
in the future, it may be possibe to do the same using
experimentalContainers
instead of useIncognitoPages
though it doesnt seem to work yetenvironmental-rose•4mo ago
You can implement the login in the handlePageFunction of Crawlee’s PlaywrightCrawler. Before navigating to any URLs, you'll first need to handle the login, and then persist the cookies or local storage to reuse the session across subsequent requests.