I've been trying to obtain contact details - emails from my connections on LinkedIn. Unfortunately, the code does not progress beyond scrolling through my connection list. Any ideas?
import re
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup as bs
import time
import random
Log in - Setup
path = "C:\\Program Files (x86)\\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get("https://www.linkedin.com")
username = driver.find_element(By.ID, 'session_key')
username.send_keys('')
password = driver.find_element(By.ID, 'session_password')
password.send_keys('')
log_in_button = driver.find_element(By.CLASS_NAME, 'sign-in-form__submit-button')
log_in_button.click()
Search Phase
driver.get("https://www.linkedin.com/mynetwork/invite-connect/connections/")
total_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(random.uniform(2.5, 4.9))
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == total_height:
break
last_height = new_height
page = bs(driver.page_source, 'html.parser')
content = page.find_all('a', {'class': "mn-connection-card__link ember-view"})
mynetwork = []
for contact in content:
mynetwork.append(contact.get('href'))
print(len(mynetwork), " connections")
my_network_emails = []
Connect to the profile of all contacts and save the email within a list
for contact in mynetwork:
driver.get("https://www.linkedin.com" + contact + "detail/contact-info/")
driver.implicitly_wait(3)
contact_page = bs(driver.page_source, 'html.parser')
content_contact_page = contact_page.find_all('a', href=re.compile("mailto"))
for contact in content_contact_page:
print("[+]", contact.get('href')[7:])
my_network_emails.append(contact.get('href')[7:])
# wait few seconds before to connect to the next profile
time.sleep(random.uniform(0.5, 1.9))
content_contact_page = contact_page.find_all('a', href=re.compile("mailto"))
for contact in content_contact_page:
print("[+]", contact.get('href')[7:])
my_network_emails.append(contact.get('href')[7:])
with open(f'network_emails.csv', 'w') as f:
writer = csv.writer(f)
for email in my_network_emails:
writer.writerow([email])