This project is a LinkedIn Scraper built using Selenium for web scraping and FastAPI for serving the scraped data via RESTful APIs. It allows users to fetch detailed profile and company information from LinkedIn based on the provided LinkedIn ID.
- Python 3.8+
- pip
- git
- Google Chrome
-
Clone the Repository
git clone https://github.com/matthewdonsemail-lab/linkedin-scraper-api.git cd linkedin-scraper-api -
Create and Activate Virtual Environment
python -m venv venv # On Windows: venv\Scripts\activate # On macOS/Linux: source venv/bin/activate
-
Install Dependencies
pip install -r requirements.txt
Environment Variables
Create a .env file in the root of the project:
LINKEDIN_ACCESS_TOKEN="YourLinkedInAccessToken"
LINKEDIN_ACCESS_TOKEN_EXP=AccessTokenExpiration
HEADLESS=True
- Replace
"YourLinkedInAccessToken"and"AccessTokenExpiration"with your actual LinkedIn access token and its expiration time. - Change
"HEADLESS"to False if you don't want the browser to open in headless mode.
To fetch data using the LinkedIn Scraper, you'll need a LinkedIn access token. The following steps will guide you through obtaining this token using the Cookie-Editor browser addon:
Add the Cookie-Editor addon to your browser. It's available for Chrome, Firefox, and other popular browsers.
Open your browser and log into your LinkedIn account as you normally would.
Once logged in, click on the Cookie-Editor icon in your browser's toolbar to open the addon.
- In the Cookie-Editor interface, look for a cookie named
li_at. This cookie contains your LinkedIn access token. - Click on the
li_atcookie to view its details, and copy the value. This is your LinkedIn access token. - Convert the Expiration time into numeric format (timestamp) like
1743212774.0, and copy the value. This is your LinkedIn access token expiration time.
- Token Validity: LinkedIn access tokens are temporary. Ensure to check the token's validity periodically and update it as needed.
- Privacy and Security: Handle your access token securely as it grants access to your LinkedIn data. Do not share your token publicly.
Important
If you continue to encounter issues with the access tokens being undefined or expired even after following these steps, please replace the values directly in the settings.py file.
To start the server, run the following command in the root directory of the project:
python run.pyThis command will start the Uvicorn server and make the API accessible on http://localhost:8000 by default.
The LinkedIn Scraper offers endpoints for retrieving detailed profile and company information from LinkedIn.
- GET
/profile-data/{linkedin_id}- Fetches profile information for the specified LinkedIn ID.
- Path Parameters:
linkedin_id: The unique LinkedIn ID of the profile.
- Response Format:
{ "linkedin_id": "string", "name": "string", "headline": "string", "education": { "positions": ["string"], "institutions": ["string"], "dates": ["string"] }, "experience": { "positions": ["string"], "institutions": ["string"], "dates": ["string"] } }
- GET
/company-data/{linkedin_id}- Fetches company information based on the given LinkedIn ID.
- Path Parameters:
linkedin_id: The unique LinkedIn ID of the company.
- Response Format:
{ "linkedin_id": "string", "name": "string", "industry": "string", "about": "string" }
Error handling is consistent across endpoints:
{
"detail": "Error fetching profile details"
}This response is returned with an HTTP status code of 500, indicating a server-side error during data fetching.
This project is configured for deployment on Railway.
- Push this repo to GitHub
- Create a new project on Railway and connect your GitHub repo
- Add the following environment variables in Railway dashboard:
LINKEDIN_ACCESS_TOKEN- Your LinkedInli_atcookie valueLINKEDIN_ACCESS_TOKEN_EXP- Token expiration timestampHEADLESS=True
- Deploy
Railway will automatically:
- Install Google Chrome and ChromeDriver via the included
nixpacks.toml - Use the
PORTenvironment variable - Start the server with
python run.py
railway.json- Railway deployment configurationnixpacks.toml- Installs Chrome/ChromeDriver for Selenium
api/routes.py- Contains API routesservices/candidate_scraper.py- Scraping function for individual profilesservices/company_scraper.py- Scraping function for company profilesservices/scraping_utils.py- Functions and options used in the scraping processsettings.py- Configuration settingsrailway.json- Railway deployment confignixpacks.toml- Nixpacks build config
- Ensure that the LinkedIn access token is valid and not expired to avoid authentication errors.
- The scraper requires Google Chrome to be installed (handled automatically on Railway).