-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
560 lines (436 loc) · 30.8 KB
/
main.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import os
import re
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from selenium.common.exceptions import TimeoutException as SeleniumTimeOutException
import pandas as pd
# DATA LOSS PREVENTION FOLDER
DLP_DIRECTORY = 'dlp'
def create_directory(path):
try:
os.makedirs(path, exist_ok=True)
print(f"Directory '{path}' created successfully or already exists.")
except Exception as e:
print(f"An error occurred: {e}")
def main():
# Get session details from user
session_url = input("Enter the session URL: ")
session_id = input("Enter the session ID: ")
# Set Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure the browser is in headless mode
chrome_options.add_argument("--disable-gpu") # Disable GPU hardware acceleration
chrome_options.add_argument("--no-sandbox") # Bypass OS security model
chrome_options.add_argument("--disable-dev-shm-usage") # Overcome limited resource problems
# Connect to the existing browser session
driver = webdriver.Remote(command_executor=session_url, options=chrome_options)
driver.session_id = session_id
# Navigate to the desired page
driver.get('https://www.linkedin.com/sales/lists/people/7209949800270548995?sortCriteria=CREATED_TIME&sortOrder=DESCENDING')
time.sleep(10)
coconut = True
lead_counter = 0
data = []
while coconut:
try:
# Wait for the button to appear
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="view_spotlight_for_type_ALL"]')))
# Get the button element
button = driver.find_element(By.CSS_SELECTOR, 'button[data-control-name="view_spotlight_for_type_ALL"]')
# Extract the number from the button's primary text
number_element = button.find_element(By.CSS_SELECTOR, '.artdeco-spotlight-tab__primary-text')
total_results = int(number_element.text)
except Exception as e:
total_results = 0
print("Total_results: ", total_results)
if total_results == 0:
print("Nobody to reach out to hence this program will sleep for the next two hours")
if data:
df = pd.DataFrame(data)
# Define the Excel file path
excel_file = 'scrape_output1.xlsx'
csv_file = 'scrape_output1.csv'
try:
# Write the DataFrame to an Excel file
df.to_excel(excel_file, index=False)
# Remove duplicate rows
df = pd.read_excel(excel_file)
df_cleaned = df.drop_duplicates()
df_cleaned.to_excel(excel_file, index=False)
print(f"Data scrapped has been written to {excel_file}")
except:
# If Openpxl is unavalible write the data to a CSV file
df.to_csv(csv_file, index=False)
# Remove duplicate rows
df = pd.read_csv(csv_file)
df_cleaned = df.drop_duplicates()
df_cleaned.to_csv(csv_file, index=False)
print(f"Data scrapped has been written to {csv_file}")
time.sleep(7200) # 2 hours in seconds
else:
# Wait for all table rows to appear
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'tr.artdeco-models-table-row')))
# Now get the html code
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser')
# Finding the table body
table_body = soup.find('tbody')
# Extracting rows from the table
rows = table_body.find_all('tr')
# List to store extracted data
for i, row in enumerate(rows):
# Extracting each cell in the row
cells = row.find_all('td')
# Extracting Name
try:
name = cells[0].find('a', class_='t-bold').get_text(strip=True)
except:
name = "NULL"
try:
linkedin_url = "https://www.linkedin.com" + str(cells[0].find('a', class_='t-bold')['href'])
except:
linkedin_url = "NULL"
# Extracting Role
try:
role = cells[0].find('div', style='display: -webkit-box; -webkit-box-orient: vertical; overflow: hidden; -webkit-line-clamp: 2; overflow-wrap: anywhere;').get_text(strip=True)
except:
role = "NULL"
# Extracting Company (Account)
try:
company = cells[1].find('span', {'data-anonymize': 'company-name'}).get_text(strip=True)
except:
company = "NULL"
# Extracting the Company Linkedin URL
try:
company_linkedin_url = "https://www.linkedin.com" + str(cells[1].find('a', href=lambda href: href and re.search(r'/sales/company/', href))['href'])
except:
company_linkedin_url = "NULL"
# Extracting Geography
try:
geography = cells[2].get_text(strip=True)
except:
geography = "NULL"
# Extracting Date Added
try:
date_added = cells[5].get_text(strip=True)
except:
date_added = "NULL"
# Appending extracted data to the list
data.append({
'Name': name,
'Linkedin URL': linkedin_url,
'Role': role,
'Company': company,
'Company Linkedin URL': company_linkedin_url,
'Geography': geography,
'Date Added': date_added
})
# DATA LOSS PREVENTION IS DLP LOGIC
create_directory(DLP_DIRECTORY)
print(f"{len(rows)} Rows of Data Scraped Successfully")
df = pd.DataFrame(data)
# Define the Excel file path
excel_file = os.path.join(DLP_DIRECTORY, 'scrape_output1.xlsx')
csv_file = os.path.join(DLP_DIRECTORY, 'scrape_output1.csv')
try:
# Write the DataFrame to an Excel file
df.to_excel(excel_file, index=False)
# Remove duplicate rows
df = pd.read_excel(excel_file)
df_cleaned = df.drop_duplicates()
df_cleaned.to_excel(excel_file, index=False)
print(f"{excel_file} has been updated")
except:
# If Openpxl is unavalible write the data to a CSV file
df.to_csv(csv_file, index=False)
# Remove duplicate rows
df = pd.read_csv(csv_file)
df_cleaned = df.drop_duplicates()
df_cleaned.to_csv(csv_file, index=False)
print(f"{csv_file} has been updated")
# Select all table rows
rows = driver.find_elements(By.CSS_SELECTOR, 'tr.artdeco-models-table-row')
# Iterate over each row
for i, row in enumerate(rows):
restricted = None
error_occured = None
connected_button_click = False
print("Lead: ", lead_counter + 1)
# Find the specific cell containing the button
action_cell = row.find_element(By.CSS_SELECTOR, 'td.list-people-detail-header__actions')
# Find the button within the cell
if action_cell:
button = action_cell.find_element(By.CSS_SELECTOR, 'button.artdeco-dropdown__trigger')
if button:
# Click the button
button.click()
time.sleep(2)
# Now, find and click on the "Message" button in the dropdown using partial aria-label match
dropdown_items = action_cell.find_elements(By.CSS_SELECTOR, '.artdeco-dropdown__item')
for item in dropdown_items:
aria_label = item.get_attribute('aria-label')
if aria_label and re.search(r'Message', aria_label):
item.click()
print("Clicked on Message button.")
break # Stop searching further
time.sleep(2)
# Wait for message modal to appear using regex for aria-label
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="overlay.close_overlay"]')))
try:
message_history = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div._message-padding_zovuu6')))
except Exception as e:
message_history = False
finally:
if message_history:
print("Message History exists")
time.sleep(2)
else:
print("Message History doesn't exist")
time.sleep(2)
# There is no message history so send the message
try:
subject_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//input[@placeholder="Subject (required)"]')))
subject_field.send_keys("Cost Effective, Efficient, 3PL Services & Freight Forwarding Services")
time.sleep(2)
message_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//textarea[@placeholder="Type your message here…"]')))
message_field.send_keys("""At Prime Avenue Logistics (https://primeavenuelogistics.com/), we specialize in providing top-tier 3PL solutions, including freight forwarding, pick pack ship, import/export, customs clearance, Amazon/Walmart replenishment, and warehousing services. Our straightforward pricing ensures you get value without the hassle.
Already have a 3PL or freight forwarder? We can provide you with a free no strings attached quote.
Let's chat about how we can streamline your supply chain and drive your success.
""")
time.sleep(2)
send_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Send']")))
send_button.click()
time.sleep(5)
except:
try:
restricted = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.conversation-restriction')))
except:
error_occured = "True"
#print(error_occured, "ERROR_OCCURED")
if restricted != None or error_occured == "True":
# Close the modal
time.sleep(2)
close_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="overlay.close_overlay"]')))
close_button.click()
print("Closed message modal.")
time.sleep(2)
# Open dropdown and click on connect
action_cell = row.find_element(By.CSS_SELECTOR, 'td.list-people-detail-header__actions')
try:
# Find the button within the cell
if action_cell:
button = action_cell.find_element(By.CSS_SELECTOR, 'button.artdeco-dropdown__trigger')
if button:
# Click the button
button.click()
time.sleep(2)
# Now, find and click on the "Connect" button in the dropdown using partial aria-label match
dropdown_items = action_cell.find_elements(By.CSS_SELECTOR, '.artdeco-dropdown__item')
for item in dropdown_items:
aria_label = item.get_attribute('class')
if aria_label and re.search(r'list-detail__connect-option', aria_label):
item.click()
print("Clicked on Connect button.")
connected_button_click = True
break # Stop searching further
time.sleep(2)
# Send connecting message
if not connected_button_click:
print("You have connected with this lead before")
time.sleep(2)
action_cell = row.find_element(By.CSS_SELECTOR, 'td.list-people-detail-header__actions')
#action_cell = row.find_element(By.CSS_SELECTOR, 'td.list-people-detail-header__actions')
#print("ACTION_CELL", action_cell)
# Find the button within the cell
if action_cell:
button = action_cell.find_element(By.CSS_SELECTOR, 'button.artdeco-dropdown__trigger')
if button:
# Click the button
button.click()
# click on the button again
button.click()
time.sleep(2)
# Now, find and click on the "Message" button in the dropdown using partial aria-label match
dropdown_items = action_cell.find_elements(By.CSS_SELECTOR, '.artdeco-dropdown__item')
for item in dropdown_items:
aria_label = item.get_attribute('aria-label')
if aria_label and re.search(r'Message', aria_label):
item.click()
print("Clicked on Message button.")
break # Stop searching further
time.sleep(2)
save_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Saved']")))
save_button.click()
time.sleep(2)
remove_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Need to Reach out To']")))
remove_button.click()
time.sleep(2)
test_list_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Connecting']")))
test_list_button.click()
time.sleep(2)
close_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="overlay.close_overlay"]')))
close_button.click()
print("Closed message modal.")
time.sleep(2)
lead_counter = lead_counter + 1
continue
else:
connect_modal = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div[data-sn-view-name="subpage-connect-modal"]')))
except:
print("You have either Connected with this lead before or An unknown Error occured. Saving this lead to the 'Connecting' list")
time.sleep(2)
action_cell = row.find_element(By.CSS_SELECTOR, 'td.list-people-detail-header__actions')
#action_cell = row.find_element(By.CSS_SELECTOR, 'td.list-people-detail-header__actions')
# Find the button within the cell
if action_cell:
button = action_cell.find_element(By.CSS_SELECTOR, 'button.artdeco-dropdown__trigger')
if button:
# Click the button
button.click()
time.sleep(2)
# Now, find and click on the "Message" button in the dropdown using partial aria-label match
dropdown_items = action_cell.find_elements(By.CSS_SELECTOR, '.artdeco-dropdown__item')
for item in dropdown_items:
aria_label = item.get_attribute('aria-label')
if aria_label and re.search(r'Message', aria_label):
item.click()
print("Clicked on Message button.")
break # Stop searching further
time.sleep(2)
save_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Saved']")))
save_button.click()
time.sleep(2)
remove_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Need to Reach out To']")))
remove_button.click()
time.sleep(2)
test_list_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Info Retrieved']")))
test_list_button.click()
time.sleep(2)
close_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="overlay.close_overlay"]')))
close_button.click()
print("Closed message modal.")
time.sleep(2)
lead_counter = lead_counter + 1
continue
else:
message_box = connect_modal.find_element(By.CSS_SELECTOR, 'textarea#connect-cta-form__invitation')
message_box.send_keys('I wanted to connect to see if we could offer you a more Cost Effective, Efficient, 3PL Services &/or Freight Forwarding Services (https://primeavenuelogistics.com/).')
time.sleep(2)
# Check if to connect with this Lead an email is needed
try:
email_needed = connect_modal.find_element(By.CSS_SELECTOR, 'input#connect-cta-form__email')
except:
email_needed = False
if email_needed:
print("You need this Lead's email to connect")
time.sleep(2)
close_connect_modal_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-test-modal-close-btn]')))
close_connect_modal_button.click()
# Find the button within the cell
if action_cell:
button = action_cell.find_element(By.CSS_SELECTOR, 'button.artdeco-dropdown__trigger')
if button:
# Click the button
button.click()
time.sleep(2)
# Now, find and click on the "Message" button in the dropdown using partial aria-label match
dropdown_items = action_cell.find_elements(By.CSS_SELECTOR, '.artdeco-dropdown__item')
for item in dropdown_items:
aria_label = item.get_attribute('aria-label')
if aria_label and re.search(r'Message', aria_label):
item.click()
print("Clicked on Message button.")
break # Stop searching further
time.sleep(2)
save_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Saved']")))
save_button.click()
time.sleep(2)
remove_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Need to Reach out To']")))
remove_button.click()
time.sleep(2)
test_list_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Info Retrieved']")))
test_list_button.click()
time.sleep(2)
close_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="overlay.close_overlay"]')))
close_button.click()
print("Closed message modal.")
time.sleep(2)
lead_counter = lead_counter + 1
continue
invitation_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Send Invitation']")))
invitation_button.click()
time.sleep(2)
# NOW ADD THE LEAD TO THE CONNECTING LIST AND REMOVE FROM THE NEED TO REACH OUT LIST
action_cell = row.find_element(By.CSS_SELECTOR, 'td.list-people-detail-header__actions')
# Find the button within the cell
if action_cell:
button = action_cell.find_element(By.CSS_SELECTOR, 'button.artdeco-dropdown__trigger')
if button:
# Click the button
button.click()
time.sleep(2)
# Now, find and click on the "Message" button in the dropdown using partial aria-label match
dropdown_items = action_cell.find_elements(By.CSS_SELECTOR, '.artdeco-dropdown__item')
for item in dropdown_items:
aria_label = item.get_attribute('aria-label')
if aria_label and re.search(r'Message', aria_label):
item.click()
print("Clicked on Message button.")
break # Stop searching further
time.sleep(2)
save_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Saved']")))
save_button.click()
time.sleep(2)
remove_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Need to Reach out To']")))
remove_button.click()
time.sleep(2)
#TODO: Add the code for the wide Linkedin Sales Navigator error
# Add to Connecting list
connecting_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Connecting']")))
connecting_button.click()
""" # Add to test list
test_list_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Test List']")))
test_list_button.click() """
""" # Add to Emailed list
emailed_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Emailed']")))
emailed_button.click() """
close_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="overlay.close_overlay"]')))
close_button.click()
print("Closed message modal.")
time.sleep(2)
lead_counter = lead_counter + 1
continue
else:
# Remove from "Need to reach out to" List
save_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Saved']")))
save_button.click()
time.sleep(2)
remove_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Need to Reach out To']")))
remove_button.click()
time.sleep(2)
#TODO: Add the code for the wide Linkedin Sales Navigator error
""" # Add to Connecting list
connecting_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Connecting']")))
connecting_button.click() """
""" # Add to test list
test_list_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Test List']")))
test_list_button.click() """
# Add to Emailed list
emailed_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Emailed']")))
emailed_button.click()
close_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button[data-control-name="overlay.close_overlay"]')))
close_button.click()
print("Closed message modal.")
time.sleep(2)
lead_counter = lead_counter + 1
driver.refresh()
time.sleep(10)
print('Page Reloaded successfully. Resuming Task...')
if __name__ == "__main__":
main()