In this tutorial, we will learn how to crawl a password protected website online by passing the username, password in agent configuration for authentication programmatically and then crawl the behind login webpages.
To crawl a website with login, first of all we must get authenticated our scraping agent with
Password. And then, we can scrape the internal pages as we do with public websites. Scraping the web with Agenty, hosted app is pretty easy and quick to setup using the extension and then we can enter the credentials by editing the scraper in agent editor. This tutorial shows, how to get data from a password protected website after login successfully and then schedule the scraper, to scrape website with login automatically on scheduled time.
There are 2 types of authentications:
- Form Authentication
- Http Authentication (also called as Basic or Network Authentication)
The Form-based authentication is most widely used website protection technique, where the websites display a HTML web form to fill in the username, password and click on submit button in order to login and access the secure pages or service. A usual password protected website scraping with form-authentication workflow looks like below and we need to perform all steps one by one to get data from website that requires a login.
Navigateto login page.
- Enter the
Usernamein input filed
- Enter the
Passwordin input field
- Click on the
- Start scraping internal pages.
The form-authentication engine in scraping agent has the following commands to interact with a login page using CSS Selectors as the target of any element, this will allow us to complete the initial 1-4 login steps prior to start scraping internal pages.
To navigate a particular webpage, for example the login page for authentication
- Value: A valid URL to navigate.
To type some text in a text box. For example, username or password to the text box
- Target: A valid CSS selector of text box.
- Value: Value to enter in the text box.
To click on a button or a hyper-link
- Target: A valid CSS selector of button/link needs to be clicked
To wait (n) seconds before firing the next event
- Value: Seconds(n) to wait
Select an item from dropdown list
- Target: A valid CSS selector of dropdown box.
- Value: Value needs to be selected.
To clear a text box
- Target: A valid CSS selector of text box, drop down to clear.
To inject a
- Value: A valid
To submit a form (or to press the Enter key)
How to crawl a website with login
Follow these steps to scrape data behind a login :
- Click on
- Go to Login to website section and Enable login as in the screenshot below.
Now go to website you want to login, and check the web page source to analyse the login form. For this tutorial, I’m going to use the cloud.agenty.com itself to demonstrate, so these will the steps I will add to my agent to login successfully.
- Navigate to
- Enter user name on text box with CSS selector
- Enter password on text box with CSS selector
- Click on the Sign In button with CSS selector
The target CSS selector can be written with name, class or id. For example, to click on the “Sign In” button all these selectors are valid.
Once the login configuration part has been completed, save the scraping agent, and scroll up to main agent page to start and test your agent. It’s always a best practice to test with few URLs ensure that the agent is login successfully before running a large job. For example, I entered some internal URLs in input which was accessible after login only, and then started the scraping job.
Agenty recommends to run a test job for few URLs, when the agent configuration has been changed. As that will allow to analyse the result to ensure everything is working as expected, instead starting the agent for big list of URLs.
Then click on the “Start” button to start the web scraping agent job.
2019-04-18 16:20:53.6110 TRACE Logging in via: FORM authentication 2019-04-18 16:20:53.6110 TRACE Running Login steps 2019-04-18 16:20:53.6266 TRACE Command: NAVIGATE, Selector: , Value: https://cloud.agenty.com/ 2019-04-18 16:20:57.3366 TRACE Command: TYPE, Selector: #ContentPlaceHolder1_LoginPanel_UserName, Value: email@example.com 2019-04-18 16:20:57.5134 TRACE Command: TYPE, Selector: #ContentPlaceHolder1_LoginPanel_Password, Value: xxxxxxxx 2019-04-18 16:20:57.5466 TRACE Command: CLICK, Selector: #ContentPlaceHolder1_LoginPanel_LoginButton, Value: 2019-04-18 16:20:57.7136 TRACE Command: WAIT, Selector: , Value: 5 2019-04-18 16:21:02.7370 TRACE https://cloud.agenty.com/app/agents 2019-04-18 16:21:03.3620 TRACE StatusCode: 200
It will take few seconds to initialize and login, then the web scraping agent will start scraping internal pages, and we can see the progress, logs and the final result as per your fields selection in the result output table.
The HTTP or basic authentication is a simple challenge which a web server can request authentication information (typically a
Password) from a client. These websites doesn’t have HTML web form to type credentials or select using CSS selectors and then click on submit button. Instead the browser open a popup dialog (as in screenshot below) asking for credentials when you visit the secured pages and then the browser use those credentials to convert into a
base64 string and send the Authorization header to server to attempt the login.
In order to crawl the basic-authentication protected websites, we need to use the
HTTP-authentication as the type of login in scraping agent and then supply the credentials with these commands:
- Edit the scraping agent by clicking on “Edit” tab
- Go to Login to website section
- Enable the Login to website feature and select the Authentication type as “HTTP-authentication”
- Add the Navigate command to go to the login page URL.
- Add the Type command with target as
usernameand value as your actual username to login.
- Add the Type command with target as
passwordand value as your actual password to login.
- Save the scraping agent and re-run.
- See the output or logs to ensure the agent is able to login successfully.
The scraping agent automatically logout to the domain after 20 minutes of in-activity on same domain or if the scraping jobs is completed prior to that. So, if you are using throttling feature to delay in sequential requests, be sure there is no gap of more than 20 minutes.
Basic Authentication with FORM
We can also get our agent session authenticated by sending a
Navigate request with username and password in the URL itself. Just make a first request using form authentication or Form submit feature with URL format below:
For example :
- When crawling password protected websites, we recommend to spent some time in analysis first, and try to use the specific login page of website instead dialog box or popup login when possible. You may find that by logging in and then logging out, most of the website auto-redirect users on specific login page when logged out.
- Add a 5-10 seconds of wait after clicking on “Login” button to give enough time to website for auto redirect to main or home page.
Recommendation: Use the
Wants to extract data behind login? Let the Agenty team setup, execute and maintain your data scraping project - Request a quote