File size: 12,545 Bytes
178c98f
 
 
 
 
 
2e93556
 
 
 
 
 
 
 
 
 
 
 
 
178c98f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e93556
178c98f
2e93556
 
178c98f
 
 
2e93556
 
 
 
178c98f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2488cfd
 
 
 
178c98f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e93556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from bs4 import BeautifulSoup
import time
import re
from urllib.parse import urlparse, urljoin
import sqlite3

# Optional Selenium imports for advanced scraping
try:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from webdriver_manager.chrome import ChromeDriverManager
    SELENIUM_AVAILABLE = True
except ImportError:
    SELENIUM_AVAILABLE = False
    print("⚠️ Selenium not available. Company research will use basic scraping only.")

class LinkedInScraper:
    def __init__(self, timeout=10, use_selenium=False):
        self.timeout = timeout
        self.use_selenium = use_selenium
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
        })
        
        if self.use_selenium and SELENIUM_AVAILABLE:
            self._setup_selenium()
        elif self.use_selenium and not SELENIUM_AVAILABLE:
            print("⚠️ Selenium requested but not available. Falling back to basic scraping.")
    
    def _setup_selenium(self):
        """Setup Selenium WebDriver"""
        if not SELENIUM_AVAILABLE:
            print("⚠️ Selenium not available. Cannot setup WebDriver.")
            return
            
        try:
            chrome_options = Options()
            chrome_options.add_argument('--headless')
            chrome_options.add_argument('--no-sandbox')
            chrome_options.add_argument('--disable-dev-shm-usage')
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument('--window-size=1920,1080')
            
            self.driver = webdriver.Chrome(
                ChromeDriverManager().install(),
                options=chrome_options
            )
        except Exception as e:
            print(f"Error setting up Selenium: {e}")
            self.use_selenium = False
    
    def _get_cached_data(self, url):
        """Check if URL data is cached in database"""
        try:
            conn = sqlite3.connect('leads.db')
            cursor = conn.cursor()
            
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS scraped_cache (
                    url TEXT PRIMARY KEY,
                    content TEXT,
                    scraped_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )
            ''')
            
            cursor.execute('SELECT content FROM scraped_cache WHERE url = ?', (url,))
            result = cursor.fetchone()
            conn.close()
            
            return result[0] if result else None
        except Exception as e:
            print(f"Cache error: {e}")
            return None
    
    def _cache_data(self, url, content):
        """Cache scraped data"""
        try:
            conn = sqlite3.connect('leads.db')
            cursor = conn.cursor()
            
            cursor.execute('''
                INSERT OR REPLACE INTO scraped_cache (url, content)
                VALUES (?, ?)
            ''', (url, content))
            
            conn.commit()
            conn.close()
        except Exception as e:
            print(f"Cache save error: {e}")
    
    def scrape_with_requests(self, url):
        """Scrape URL using requests and BeautifulSoup"""
        try:
            response = self.session.get(url, timeout=self.timeout)
            response.raise_for_status()
            
            soup = BeautifulSoup(response.content, 'html.parser')
            
            # Extract various content types
            content_parts = []
            
            # Try to get meta description
            meta_desc = soup.find('meta', attrs={'name': 'description'})
            if meta_desc:
                content_parts.append(f"Description: {meta_desc.get('content', '')}")
            
            # Try to get title
            title = soup.find('title')
            if title:
                content_parts.append(f"Title: {title.get_text().strip()}")
            
            # Try to get about section or main content
            about_selectors = [
                '.about-section',
                '.company-description',
                '.about-us',
                '[class*="about"]',
                '.description',
                '.summary',
                'main',
                '.content'
            ]
            
            for selector in about_selectors:
                elements = soup.select(selector)
                for element in elements:
                    text = element.get_text().strip()
                    if len(text) > 50:  # Only meaningful content
                        content_parts.append(text[:500])  # Limit length
                        break
                if content_parts:
                    break
            
            # If no specific content found, get paragraphs
            if not content_parts:
                paragraphs = soup.find_all('p')
                for p in paragraphs[:3]:  # First 3 paragraphs
                    text = p.get_text().strip()
                    if len(text) > 30:
                        content_parts.append(text[:300])
            
            return ' | '.join(content_parts) if content_parts else "No content extracted"
            
        except Exception as e:
            return f"Error scraping {url}: {str(e)}"
    
    def scrape_with_selenium(self, url):
        """Scrape URL using Selenium"""
        try:
            self.driver.get(url)
            WebDriverWait(self.driver, self.timeout).until(
                EC.presence_of_element_located((By.TAG_NAME, "body"))
            )
            
            # Wait a bit for dynamic content
            time.sleep(2)
            
            content_parts = []
            
            # Try different selectors for LinkedIn
            linkedin_selectors = [
                '[data-test-id="about-us-description"]',
                '.company-about-us-description',
                '.about-section',
                '[class*="about"]'
            ]
            
            for selector in linkedin_selectors:
                try:
                    elements = self.driver.find_elements(By.CSS_SELECTOR, selector)
                    for element in elements:
                        text = element.text.strip()
                        if len(text) > 50:
                            content_parts.append(text[:500])
                            break
                except:
                    continue
            
            # If no LinkedIn-specific content, try general selectors
            if not content_parts:
                general_selectors = ['main', '.content', 'article', '.description']
                for selector in general_selectors:
                    try:
                        elements = self.driver.find_elements(By.CSS_SELECTOR, selector)
                        for element in elements:
                            text = element.text.strip()
                            if len(text) > 50:
                                content_parts.append(text[:500])
                                break
                    except:
                        continue
            
            return ' | '.join(content_parts) if content_parts else "No content extracted"
            
        except Exception as e:
            return f"Error scraping {url} with Selenium: {str(e)}"
    
    def scrape_linkedin_profile(self, linkedin_url):
        """Scrape LinkedIn company profile"""
        if not linkedin_url or not linkedin_url.strip():
            return "No LinkedIn URL provided"
        
        # Check cache first
        cached_content = self._get_cached_data(linkedin_url)
        if cached_content:
            return cached_content
        
        try:
            # Clean URL
            linkedin_url = linkedin_url.strip()
            if not linkedin_url.startswith('http'):
                linkedin_url = 'https://' + linkedin_url
            
            # Use appropriate scraping method
            if self.use_selenium:
                content = self.scrape_with_selenium(linkedin_url)
            else:
                content = self.scrape_with_requests(linkedin_url)
            
            # Cache the result
            self._cache_data(linkedin_url, content)
            
            return content
            
        except Exception as e:
            return f"Error accessing LinkedIn: {str(e)}"
    
    def scrape_linkedin_company(self, linkedin_url):
        """Alias for scrape_linkedin_profile - for compatibility"""
        return self.scrape_linkedin_profile(linkedin_url)
    
    def scrape_company_data(self, linkedin_url):
        """Another alias for compatibility"""
        return self.scrape_linkedin_profile(linkedin_url)
    
    def scrape_company_website(self, company_name):
        """Scrape company website as fallback"""
        try:
            # Try to construct company website URL
            company_clean = re.sub(r'[^\w\s-]', '', company_name.lower())
            company_clean = re.sub(r'\s+', '', company_clean)
            
            possible_urls = [
                f"https://{company_clean}.com",
                f"https://www.{company_clean}.com",
                f"https://{company_clean}.org",
                f"https://www.{company_clean}.org"
            ]
            
            for url in possible_urls:
                cached_content = self._get_cached_data(url)
                if cached_content:
                    return cached_content
                
                try:
                    if self.use_selenium:
                        content = self.scrape_with_selenium(url)
                    else:
                        content = self.scrape_with_requests(url)
                    
                    if "Error" not in content and len(content) > 50:
                        self._cache_data(url, content)
                        return content
                except:
                    continue
            
            return f"Could not find website for {company_name}"
            
        except Exception as e:
            return f"Error finding company website: {str(e)}"
    
    def scrape_linkedin_or_company(self, linkedin_url, company_name):
        """Main method to scrape LinkedIn or fallback to company website"""
        # First try LinkedIn
        if linkedin_url and linkedin_url.strip():
            linkedin_content = self.scrape_linkedin_profile(linkedin_url)
            if "Error" not in linkedin_content and len(linkedin_content) > 50:
                return f"LinkedIn: {linkedin_content}"
        
        # Fallback to company website
        company_content = self.scrape_company_website(company_name)
        return f"Company Website: {company_content}"
    
    def __del__(self):
        """Clean up Selenium driver"""
        if hasattr(self, 'driver'):
            try:
                self.driver.quit()
            except:
                pass


# Standalone function for easy import
def scrape_company_info(input_data):
    """
    Scrape company information from LinkedIn URL or company name
    
    Args:
        input_data (str): LinkedIn URL or company name
        
    Returns:
        str: Scraped company information or error message if dependencies missing
    """
    if not SELENIUM_AVAILABLE:
        return "Company research feature requires additional setup. Please install selenium and webdriver-manager for enterprise features."
        
    try:
        scraper = LinkedInScraper()
        
        # Check if input is a LinkedIn URL
        if 'linkedin.com' in input_data.lower():
            result = scraper.scrape_linkedin_or_company(input_data, "")
        else:
            # Treat as company name
            result = scraper.scrape_company_website(input_data)
        
        return result if result else ""
        
    except Exception as e:
        print(f"Error in scrape_company_info: {e}")
        return ""