-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_to_html.py
More file actions
76 lines (59 loc) · 2.75 KB
/
Copy pathurl_to_html.py
File metadata and controls
76 lines (59 loc) · 2.75 KB
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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import re
import time
def sign_in_to_linkedin(driver,email, password):
# Navigate to the LinkedIn login page
driver.get('https://www.linkedin.com/login')
# Enter your email address and password
email_input = driver.find_element(By.ID, 'username')
password_input = driver.find_element(By.ID, 'password')
email_input.send_keys(email)
password_input.send_keys(password)
password_input.send_keys(Keys.ENTER)
def get_html_from_linkedin_url(driver,url:None)->str:
try:
driver.get(url)
time.sleep(5) # Allow time for the page to load and render JavaScript
html_content = driver.page_source
except Exception as e:
print(f"Error with Selenium: {e}")
finally:
driver.quit() # Close the browser
return html_content
def extract_text_from_html(html: str) -> str:
"""Try to extract the main job description from LinkedIn job HTML. This is heuristic."""
soup = BeautifulSoup(html, "html.parser")
# LinkedIn job description often sits in elements with class names containing 'description' or 'job-description'
candidates = soup.find_all(lambda tag: tag.name in ("div", "section") and tag.get("class"))
best_text = []
for c in candidates:
classes = " ".join(c.get("class") or [])
if "description" in classes or "job" in classes or "job-description" in classes or "description__text" in classes:
text = c.get_text(separator="\n", strip=True)
if text and len(text) > 200:
best_text.append(text)
# fallback: whole page text
if not best_text:
return soup.get_text(separator="\n", strip=True)
# choose the largest block
best_text.sort(key=len, reverse=True)
return best_text[0]
def job_text_for_linkedin_url(url: str) -> str:
"""Fetch HTML from a LinkedIn job URL."""
# Set up Chrome options for headless browsing (optional)
chrome_options = Options()
chrome_options.add_argument("--headless") # Run Chrome in headless mode (no UI)
chrome_options.add_argument("--disable-gpu") # Recommended for headless mode
# Set up ChromeDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# Sign in to LinkedIn
sign_in_to_linkedin(driver, 'sample_email', 'sample_password')
# Get HTML from LinkedIn job URL
html_content = get_html_from_linkedin_url(driver,url)
return extract_text_from_html(html_content)