ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/Redgrave/vCardSearchAndDownload.py
Revision: 874
Committed: Tue Dec 31 01:27:14 2024 UTC (14 months, 3 weeks ago) by nino.borges
Content type: text/x-python
File size: 3301 byte(s)
Log Message:
This version reads from a file with many email addresses and worked perfectly for all of the KLGATES addresses.

File Contents

# User Rev Content
1 nino.borges 873 """
2    
3     vCardSearchAndDownload
4    
5     Created by:
6     Emanuel Borges
7     12.30.2024
8    
9     This program uses chromedriver to first search for a person on a public company people search and then, if found, download the vCard for that person.
10    
11     """
12    
13     import os
14     import time
15     from selenium import webdriver
16     from selenium.webdriver.common.by import By
17     from selenium.webdriver.common.keys import Keys
18     from urllib.parse import urljoin
19     import requests
20    
21    
22 nino.borges 874 #BASE_URL = "https://perkinscoie.com/people-search"
23 nino.borges 873 BASE_URL = "https://www.klgates.com/people"
24 nino.borges 874 #BASE_URL = "https://www.hoganlovells.com/en/our-people"
25 nino.borges 873
26     VCARD_DIR = "vcards"
27     os.makedirs(VCARD_DIR, exist_ok = True)
28    
29    
30     driver = webdriver.Chrome()
31    
32 nino.borges 874 version = '0.2.0'
33 nino.borges 873
34    
35     def search_and_download_vcards(person_name):
36     try:
37     driver.get(BASE_URL)
38    
39     time.sleep(3)
40    
41    
42     search_box = driver.find_element(By.ID, "searchbox")
43 nino.borges 874 #search_box = driver.find_element(By.ID, "edit-keyword")
44     #search_box = driver.find_element(By.ID, "name3")
45 nino.borges 873 search_box.clear()
46 nino.borges 874 #search_box.send_keys(person_name)
47     search_box.send_keys(f'"{person_name}"')
48 nino.borges 873 search_box.send_keys(Keys.RETURN)
49    
50     time.sleep(3)
51    
52    
53     results = driver.find_elements(By.CSS_SELECTOR, "a[href*='vcard']")
54 nino.borges 874
55 nino.borges 873 if not results:
56     print(f"No vCards found for '{person_name}'.")
57     return
58    
59    
60     for link in results:
61     vcard_url = link.get_attribute("href")
62     download_vcard(vcard_url,person_name)
63    
64     except Exception as e:
65     print(f"Error during search or download: {e}")
66 nino.borges 874 ## finally:
67     ## driver.quit()
68 nino.borges 873
69    
70    
71     def download_vcard(vcard_url, person_name):
72     try:
73     response = requests.get(vcard_url)
74     response.raise_for_status()
75    
76     #filename = os.path.basename(vcard_url)
77     filename = f"{person_name}.vcf"
78     filepath = os.path.join(VCARD_DIR,filename)
79    
80    
81     with open(filepath,"wb") as file:
82     file.write(response.content)
83     print(f"Downloaded vCard: {filepath}")
84     except Exception as e:
85     print(f"Error downloading vCard from {vcard_url}: {e}")
86    
87    
88     if __name__ == '__main__':
89 nino.borges 874 #inputFilePath = r"C:\Users\eborges\OneDrive - Redgrave LLP\Documents\Cases\Amazon\MAL-NamesToFind\KLGATES.txt"
90     inputFilePath = r"C:\Users\eborges\OneDrive - Redgrave LLP\Documents\Cases\Amazon\MAL-NamesToFind\KLGATES_SUB3.txt"
91     #inputFilePath = r"C:\Users\eborges\OneDrive - Redgrave LLP\Documents\Cases\Amazon\MAL-NamesToFind\TEST.txt"
92 nino.borges 873
93 nino.borges 874
94    
95     ## This can be a list of any value to search on but for now I need it to be unique, which is why I'm searching only on email addresses
96     listOfEmailAddresses = set()
97    
98    
99     contents = open(inputFilePath).readlines()
100     for line in contents:
101     line = line.replace("\n","")
102     uniqueRowNumb,emailAddr = line.split("|")
103     person_to_search = emailAddr
104     search_and_download_vcards(person_to_search)
105    
106    
107 nino.borges 873
108 nino.borges 874
109 nino.borges 873 #person_to_search = "Gaia Bacchi"
110     #person_to_search = "tim.weston@klgates.com"
111 nino.borges 874 #person_to_search = "BPeters@perkinscoie.com"
112     #person_to_search = "Katie.McMullan@hoganlovells.com"
113     #search_and_download_vcards(person_to_search)
114    
115     driver.quit()