ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/Redgrave/vCardSearchAndDownload.py
Revision: 873
Committed: Mon Dec 30 21:18:00 2024 UTC (14 months, 3 weeks ago) by nino.borges
Content type: text/x-python
File size: 2191 byte(s)
Log Message:
First working version of a program that will connect to a company people search website, like KLGATES, and perform searches based on something like email address.  If that person is found, it will download the vcard.  This version works perfectly for that site, although just one at a time.

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     #BASE_URL = "https://www.klgates.com/people#LangCode=en-US"
23     BASE_URL = "https://www.klgates.com/people"
24    
25     VCARD_DIR = "vcards"
26     os.makedirs(VCARD_DIR, exist_ok = True)
27    
28    
29     driver = webdriver.Chrome()
30    
31     version = '0.1.0'
32    
33    
34     def search_and_download_vcards(person_name):
35     try:
36     driver.get(BASE_URL)
37    
38     time.sleep(3)
39    
40    
41     search_box = driver.find_element(By.ID, "searchbox")
42     search_box.clear()
43     search_box.send_keys(person_name)
44     search_box.send_keys(Keys.RETURN)
45    
46     time.sleep(3)
47    
48    
49     results = driver.find_elements(By.CSS_SELECTOR, "a[href*='vcard']")
50     if not results:
51     print(f"No vCards found for '{person_name}'.")
52     return
53    
54    
55     for link in results:
56     vcard_url = link.get_attribute("href")
57     download_vcard(vcard_url,person_name)
58    
59     except Exception as e:
60     print(f"Error during search or download: {e}")
61     finally:
62     driver.quit()
63    
64    
65    
66     def download_vcard(vcard_url, person_name):
67     try:
68     response = requests.get(vcard_url)
69     response.raise_for_status()
70    
71     #filename = os.path.basename(vcard_url)
72     filename = f"{person_name}.vcf"
73     filepath = os.path.join(VCARD_DIR,filename)
74    
75    
76     with open(filepath,"wb") as file:
77     file.write(response.content)
78     print(f"Downloaded vCard: {filepath}")
79     except Exception as e:
80     print(f"Error downloading vCard from {vcard_url}: {e}")
81    
82    
83     if __name__ == '__main__':
84    
85    
86    
87     #person_to_search = "Gaia Bacchi"
88     #person_to_search = "tim.weston@klgates.com"
89     person_to_search = "hilarie.sander@klgates.com"
90     search_and_download_vcards(person_to_search)