-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_linkedin_profile_scraper.py
73 lines (61 loc) · 2.33 KB
/
simple_linkedin_profile_scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(r'chromedriver.exe')
## Log In to LinkedIn
def login():
username = input('Enter your LinkedIn username: ')
password = input('Enter your LinkedIn password: ')
username = driver.find_element_by_id('username')
username.send_keys()
password = driver.find_element_by_id('password')
password.send_keys()
log_in_button = driver.find_element_by_xpath('//*[@id="organic-div"]/form/div[3]/button')
log_in_button.click()
## Get All Links on page
links = []
def all_links():
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
if '/in/' in elem.get_attribute("href"):
links.append (elem.get_attribute("href"))
all_links()
## remove duplicates in list
links = list(dict.fromkeys(links))
names = []
designations = []
locations = []
emps = []
educations = []
profile_link = []
## extract information from all urls and add items to global list
for link in links:
driver.get(link)
time.sleep(5)
name = driver.find_element_by_css_selector('.inline.t-24.t-black.t-normal.break-words')
designation = driver.find_element_by_css_selector('.mt1.t-18.t-black.t-normal.break-words')
try:
location = driver.find_element_by_css_selector('.t-16.t-black.t-normal.inline-block')
except:
location = "not available"
emp = driver.find_elements_by_css_selector(".pv-profile-section__card-item-v2.pv-profile-section.pv-position-entity.ember-view")
education = driver.find_elements_by_css_selector("#education-section > ul")
profile_link.append(link)
names.append(name.text)
designations.append(designation.text)
locations.append(location.text)
for x in emp:
emps.append(x.text.splitlines())
for y in education:
educations.append(y.text.splitlines())
print ("{}'s information saved".format(name.text))
print ("All done !")
#for CSV
with open(newfilePath, "w", encoding ='utf-8') as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
#for excel
import pandas as pd
df = pd.DataFrame(zip(names, designations, locations, emps, educations, profile_link), columns=['Name', 'Profile', 'Location', 'Employment', 'Education', 'Profile url'])
df.to_excel('test.xlsx', index = False)