maximo3k / linkedin-sales-navigator-scraper

LinkedIn Sales Navigator saved Search into CSV
GNU General Public License v3.0
4 stars 2 forks source link

getting the users Linkedin url #1

Open Andy1996247 opened 1 month ago

Andy1996247 commented 1 month ago

this is a great tool and i will use it alot. in my case i need to get the users LinkedIn profile URL not the sales navigator url. i modified the code. it need improvement but its a start. let me know your thoughts.

def scroll_extract(driver, items): print("Starting scroll and extract process...") results = [] profiles_processed = 0 # Counter to keep track of profiles processed

for index, item in enumerate(items):
    print(f"Processing item {index + 1}/{len(items)}")
    # initialize variables
    person_name = "NA"
    person_title = "NA"
    person_company = "NA"
    person_location = "NA"
    person_link = "NA"

    try:
        # Scroll the item into view using JavaScript
        driver.execute_script("arguments[0].scrollIntoView(true);", item)
        print(f"Scrolled to item {index + 1}")
        time.sleep(1)  # Wait 3 seconds after scrolling

        # wait till visible
        WebDriverWait(driver, 10).until(EC.visibility_of(item))
        print(f"Item {index + 1} is visible.")
        time.sleep(2)  # Wait 3 seconds after item is visible

        item = driver.find_elements(By.CSS_SELECTOR, "li.artdeco-list__item.pl3.pv3")[index]

        # Extract person's name safely
        name_element = item.find_element(By.CSS_SELECTOR, "span[data-anonymize='person-name']")
        person_name = name_element.text if name_element else "NA"
        print(f"Extracted name: {person_name}")
        time.sleep(0)  # Wait 3 seconds after extracting name

        # Click on the profile link to open the profile
        link_element = name_element.find_element(By.XPATH, "..")
        driver.execute_script("arguments[0].click();", link_element)
        print(f"Clicked on profile link for {person_name}")
        time.sleep(1)  # Wait 3 seconds after clicking profile link

        # Wait for the profile page to load
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "body")))
        print(f"Profile page loaded for {person_name}")
        time.sleep(3)  # Wait 3 seconds after profile page loads

        # Click the actions overflow menu
        actions_button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "._actions-container_sqh8tm [aria-label='Open actions overflow menu']"))
        )
        driver.execute_script("arguments[0].click();", actions_button)
        print(f"Clicked actions overflow menu for {person_name}")
        time.sleep(2)  # Wait 3 seconds after clicking actions overflow menu

       # Click the "Copy LinkedIn.com URL" button
        copy_url_button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//button[contains(., 'Copy LinkedIn.com URL')]"))
        )
        copy_url_button.click()
        print(f"Clicked 'Copy LinkedIn.com URL' for {person_name}")
        time.sleep(1)  # Wait 3 seconds after clicking copy URL button

        # Wait for the URL to be copied to the clipboard
        time.sleep(1)
        print("Waited for URL to be copied to clipboard.")

        # Retrieve the URL from the clipboard
        person_link = pyperclip.paste()
        print(f"Copied LinkedIn URL for {person_name}: {person_link}")
        time.sleep(1)  # Wait 3 seconds after copying URL

        # Click to return to the search results
        search_results_button = driver.find_element(By.CSS_SELECTOR, "[data-sn-view-name='module-lead-search-results'] .overflow-x-hidden")
        driver.execute_script("arguments[0].click();", search_results_button)
        print(f"Clicked to return to search results for {person_name}")
        time.sleep(2)  # Wait 3 seconds after clicking to return to search results

        # Extract person's title safely
        title_element = item.find_element(By.CSS_SELECTOR, "span[data-anonymize='title']")
        person_title = title_element.text if title_element else "NA"
        print(f"Extracted title: {person_title}")
        time.sleep(0)  # Wait 3 seconds after extracting title

        # Extract company name safely with retries
        retries = 3
        while retries > 0:
            try:
                company_element = item.find_element(By.CSS_SELECTOR, "a[data-anonymize='company-name']")
                person_company = company_element.text if company_element else "NA"
                print(f"Extracted company: {person_company}")
                break
            except NoSuchElementException:
                print(f"Retrying to find company name for {person_name}...")
                time.sleep(0)
                retries -= 1
                if retries == 0:
                    print(f"Failed to find company name for {person_name} after retries.")
                    person_company = "NA"

        # Extract location safely
        location_element = item.find_element(By.CSS_SELECTOR, "span[data-anonymize='location']")
        person_location = location_element.text if location_element else "NA"
        print(f"Extracted location: {person_location}")
        time.sleep(0)  # Wait 3 seconds after extracting location

        results.append({
            'person_name': person_name,
            'person_title': person_title,
            'person_company': person_company,
            'person_location': person_location,
            'person_link': person_link,
        })
        print(f"Appended results for {person_name}")

        profiles_processed += 1  # Increment the counter

        # Scroll after every 4 profiles
        if profiles_processed % 2 == 0:
            driver.execute_script("window.scrollBy(0, window.innerHeight);")
            print("Scrolled to next set of profiles.")
            time.sleep(2)  # Wait 3 seconds after scrolling

        # Wait for 1 second to allow any dynamic content to load
        time.sleep(1)
    except NoSuchElementException as e:
        print(f"Failed to process item at index {index}: {str(e)}")
        # You may choose to append a record with NA values or just log the error
        results.append({
            'person_name': person_name,
            'person_title': person_title,
            'person_company': person_company,  # Default NA for company as the error occurred here
            'person_location': person_location,
            'person_link': person_link,
        })
    except Exception as e:
        print(f"Failed to process item at index {index}: {str(e)}")
        # You may choose to append a record with NA values or just log the error
        results.append({
            'person_name': person_name,
            'person_title': person_title,
            'person_company': person_company,  # Default NA for company as the error occurred here
            'person_location': person_location,
            'person_link': person_link,
        })

write_results_to_csv(results, 'prospects_1.csv')
print("Finished scroll and extract process.")
return
maximo3k commented 1 month ago

Hi Andy, thansk for your comment. I went through your code, is it for the regular LinkedIn search to extract all those information?

The code I made was specifically for the SalesNavigator, because despite paying for it, they wouldn't let you export anything... Unfortunately I don't have an account anymore (free trial ended).

Andy1996247 commented 1 month ago

Hi Andy, thansk for your comment. I went through your code, is it for the regular LinkedIn search to extract all those information?

The code I made was specifically for the SalesNavigator, because despite paying for it, they wouldn't let you export anything... Unfortunately I don't have an account anymore (free trial ended).

Yes, it's for sales navigator. Do you have another linkedin account? If you do, I can send you a 2 month free trial Referral.

maximo3k commented 1 month ago

I just made a new one, https://www.linkedin.com/in/max-dev-91122a31b/

Andy1996247 commented 1 month ago

I just made a new one, https://www.linkedin.com/in/max-dev-91122a31b/

Sent you a connection request