-
Notifications
You must be signed in to change notification settings - Fork 24
/
linkedin-scrapper.py
173 lines (150 loc) · 6.52 KB
/
linkedin-scrapper.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from selenium import webdriver
import time
from bs4 import BeautifulSoup
import xlsxwriter
from tkinter import *
class Linkedin():
def getData(self):
driver = webdriver.Chrome('../chromedriver.exe')
driver.get('https://www.linkedin.com/login')
driver.find_element_by_id('username').send_keys('USER NAME') #Enter username of linkedin account here
driver.find_element_by_id('password').send_keys('PASSWORD') #Enter Password of linkedin account here
driver.find_element_by_xpath("//*[@type='submit']").click()
#*********** Search Result ***************#
search_key = "data analyst" # Enter your Search key here to find people
key = search_key.split()
keyword = ""
for key1 in key:
keyword = keyword + str(key1).capitalize() +"%20"
keyword = keyword.rstrip("%20")
global data
data = []
for no in range(1,30):
start = "&page={}".format(no)
search_url = "https://www.linkedin.com/search/results/people/?keywords={}&origin=SUGGESTION{}".format(keyword,start)
driver.get(search_url)
driver.maximize_window()
for scroll in range(2):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
search = BeautifulSoup(driver.page_source,'lxml')
peoples = search.findAll('a', attrs = {'data-control-name':'search_srp_result'})
count = 0
print("Going to scrape Page {} data".format(no))
for people in peoples:
count+=1
if count%2==0:
profile_url = "https://www.linkedin.com" + str(people['href'])
driver.get(profile_url)
# #********** Profile Details **************#
loc = ""
page = BeautifulSoup(driver.page_source,'lxml')
try:
cover = page.find('img', attrs = {'class':'profile-background-image__image relative full-width full-height'})['src']
except:
cover = 'None'
try:
profile = page.find("img", attrs = {
'class':'lazy-image pv-top-card-section__photo presence-entity__image EntityPhoto-circle-9 loaded'})['src']
except:
profile = "None"
try:
title = str(page.find("li", attrs = {'class':'inline t-24 t-black t-normal break-words'}).text).strip()
except:
title = 'None'
try:
heading = str(page.find('h2', attrs = {'class':'mt1 t-18 t-black t-normal'}).text).strip()
except:
heading = 'None'
try:
loc = str(page.find('li', attrs = {'class':'t-16 t-black t-normal inline-block'}).text).strip()
except:
heading = 'None'
#******* Contact Information **********#
time.sleep(2)
driver.get(profile_url + 'detail/contact-info/')
info = BeautifulSoup(driver.page_source, 'lxml')
details = info.findAll('section',attrs = {'class':'pv-contact-info__contact-type'})
try:
websites = details[1].findAll('a')
for website in websites:
website = website['href']
except:
website = 'None'
try:
phone = details[2].find('span').text
except:
phone = 'None'
try:
email = str(details[3].find('a').text).strip()
except:
email = 'None'
try:
connected = str(details[-1].find('span').text).strip()
except:
connected = 'None'
data.append({'profile_url':profile_url,'cover':cover,'profile':profile,'title':title,'heading':heading,'loc':loc,'website':website,'phone':phone,'email':email,'connected':connected,})
print("!!!!!! Data scrapped !!!!!!")
driver.quit()
def writeData(self):
workbook = xlsxwriter.Workbook("linkedin-search-data.xlsx")
worksheet = workbook.add_worksheet('Peoples')
bold = workbook.add_format({'bold': True})
worksheet.write(0,0,'profile_url',bold)
worksheet.write(0,1,'Name',bold)
worksheet.write(0,2,'cover',bold)
worksheet.write(0,3,'profile image',bold)
worksheet.write(0,4,'heading',bold)
worksheet.write(0,5,'location',bold)
worksheet.write(0,6,'website',bold)
worksheet.write(0,7,'phone',bold)
worksheet.write(0,8,'email',bold)
worksheet.write(0,9,'connected',bold)
for i in range(1,len(data)+1):
try:
worksheet.write(i,0,data[i]['profile_url'])
except:
pass
try:
worksheet.write(i,1,data[i]['title'])
except:
pass
try:
worksheet.write(i,2,data[i]['cover'])
except:
pass
try:
worksheet.write(i,3,data[i]['profile'])
except:
pass
try:
worksheet.write(i,4,data[i]['heading'])
except:
pass
try:
worksheet.write(i,5,data[i]['loc'])
except:
pass
try:
worksheet.write(i,6,data[i]['website'])
except:
pass
try:
worksheet.write(i,7,data[i]['phone'])
except:
pass
try:
worksheet.write(i,8,data[i]['email'])
except:
pass
try:
worksheet.write(i,9,data[i]['connected'])
except:
pass
workbook.close()
def start(self):
self.getData()
self.writeData()
if __name__ == "__main__":
obJH = Linkedin()
obJH.start()