-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedin.py
304 lines (244 loc) · 9.47 KB
/
linkedin.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- coding: utf-8 -*-
"""
Simple Linkedin crawler to collect user's profile data.
@author: idwaker
To use this you need linkedin account, all search is done through your account
Requirements:
python-selenium
python-click
python-keyring
Tested on Python 3 not sure how Python 2 behaves
"""
import sys
import csv
import time
import click
import getpass
import keyring
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
LINKEDIN_URL = 'https://www.linkedin.com/uas/login'
class UnknownUserException(Exception):
pass
class UnknownBrowserException(Exception):
pass
class WebBus:
"""
context manager to handle webdriver part
"""
def __init__(self, browser):
self.browser = browser
self.driver = None
def __enter__(self):
# XXX: This is not so elegant
# should be written in better way
if self.browser.lower() == 'firefox':
self.driver = webdriver.Firefox()
elif self.browser.lower() == 'chrome':
self.driver = webdriver.Chrome()
elif self.browser.lower() == 'phantomjs':
self.driver = webdriver.PhantomJS()
else:
raise UnknownBrowserException("Unknown Browser")
return self
def __exit__(self, _type, value, traceback):
if _type is OSError or _type is WebDriverException:
click.echo("Please make sure you have this browser")
return False
if _type is UnknownBrowserException:
click.echo("Please use either Firefox, PhantomJS or Chrome")
return False
self.driver.close()
def get_password(username):
"""
get password from stored keychain service
"""
print(username)
password = keyring.get_password('linkedinpy', username)
if not password:
raise UnknownUserException("""You need to store password for this user
first.""")
return password
def login_into_linkedin(driver, username):
"""
Just login to linkedin if it is not already loggedin
"""
userfield = driver.find_element_by_id('session_key-login')
passfield = driver.find_element_by_id('session_password-login')
submit_form = driver.find_element_by_class_name('btn-primary')
password = get_password(username)
print(password)
# If we have login page we get these fields
# I know it's a hack but it works
if userfield and passfield:
userfield.send_keys(username)
passfield.send_keys(password)
submit_form.click()
click.echo("Logging in")
def collect_names(filepath):
"""
collect names from the file given
"""
names = []
with open(filepath, 'r') as _file:
names = [line.strip() for line in _file.readlines()]
return names
@click.group()
def cli():
"""
First store password
$ python linkedin store username@example.com
Password: **
Then crawl linkedin for users
$ python linkedin crawl username@example.com with_names output.csv --browser=firefox
"""
pass
@click.command()
@click.option('--browser', default='phantomjs', help='Browser to run with')
@click.argument('username')
@click.argument('infile')
@click.argument('outfile')
def crawl(browser, username, infile, outfile):
"""
Run this crawler with specified username
"""
# first check and read the input file
#all_names = collect_names(infile)
fieldnames = ['fullname', 'locality', 'industry', 'current summary',
'past summary', 'education', ]
# then check we can write the output file
# we don't want to complete process and show error about not
# able to write outputs
with open(outfile, 'w') as csvfile:
# just write headers now
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
link_title = './/a[@class="title main-headline"]'
# now open the browser
driver = webdriver.Firefox()
driver.get(LINKEDIN_URL)
login_into_linkedin(driver, username)
search_inputs = ['telecom paristech','eurecom']
for search_input in search_inputs:
click.echo('Getting ...')
click.echo(search_input)
try:
search_input = driver.find_element_by_id('main-search-box')
click.echo('search_input')
except NoSuchElementException:
click.echo('error')
continue
search_input.send_keys(search_input)
search_form = bus.driver.find_element_by_id('global-search')
click.echo(search_form)
search_form.submit()
click.echo('Submit ...')
# for name in all_names:
# click.echo("Getting ...")
# try:
# search_input = bus.driver.find_element_by_id('main-search-box')
# except NoSuchElementException:
# continue
# search_input.send_keys(name)
# search_form = bus.driver.find_element_by_id('global-search')
# search_form.submit()
# # search_button = bus.driver.find_element_by_xpath(search_btn)
# # search_button.click()
# profiles = []
# # collect all the profile links
# results = None
# try:
# results = bus.driver.find_element_by_id('results-container')
# except NoSuchElementException:
# continue
# links = results.find_elements_by_xpath(link_title)
# # get all the links before going through each page
# links = [link.get_attribute('href') for link in links]
# for link in links:
# # XXX: This whole section should be separated from this method
# # XXX: move try-except to context managers
# bus.driver.get(link)
# overview = None
# overview_xpath = '//div[@class="profile-overview-content"]'
# try:
# overview = bus.driver.find_element_by_xpath(overview_xpath)
# except NoSuchElementException:
# click.echo("No overview section skipping this user")
# continue
# # every xpath below here are relative
# fullname = None
# fullname_xpath = './/span[@class="full-name"]'
# try:
# fullname = overview.find_element_by_xpath(fullname_xpath)
# except NoSuchElementException:
# # we store empty fullname : notsure for this
# fullname = ''
# else:
# fullname = fullname.text.strip()
# locality = None
# try:
# locality = overview.find_element_by_class_name('locality')
# except NoSuchElementException:
# locality = ''
# else:
# locality = locality.text.strip()
# industry = None
# try:
# industry = overview.find_element_by_class_name('industry')
# except NoSuchElementException:
# industry = ''
# else:
# industry = industry.text.strip()
# current_summary = None
# csummary_xpath = './/tr[@id="overview-summary-current"]/td'
# try:
# current_summary = overview.find_element_by_xpath(csummary_xpath)
# except NoSuchElementException:
# current_summary = ''
# else:
# current_summary = current_summary.text.strip()
# past_summary = None
# psummary_xpath = './/tr[@id="overview-summary-past"]/td'
# try:
# past_summary = overview.find_element_by_xpath(psummary_xpath)
# except NoSuchElementException:
# past_summary = ''
# else:
# past_summary = past_summary.text.strip()
# education = None
# education_xpath = './/tr[@id="overview-summary-education"]/td'
# try:
# education = overview.find_element_by_xpath(education_xpath)
# except NoSuchElementException:
# education = ''
# else:
# education = education.text.strip()
# data = {
# 'fullname': fullname,
# 'locality': locality,
# 'industry': industry,
# 'current summary': current_summary,
# 'past summary': past_summary,
# 'education': education,
# }
# profiles.append(data)
# with open(outfile, 'a+') as csvfile:
# writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# writer.writerows(profiles)
# click.echo("Obtained ..." + name)
@click.command()
@click.argument('username')
def store(username):
"""
Store given password for this username to keystore
"""
passwd = getpass.getpass()
keyring.set_password('linkedinpy', username, passwd)
click.echo("Password updated successfully")
cli.add_command(crawl)
cli.add_command(store)
if __name__ == '__main__':
cli()