-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
41 lines (35 loc) · 1.23 KB
/
scrape.py
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
import selenium.webdriver as webdriver
from selenium.webdriver.chrome.service import Service
import time
from bs4 import BeautifulSoup
def scrape_website(website):
print("Launching browser...🚀")
chrome_driver_path = "./chromedriver"
driver = webdriver.Chrome(service=Service(chrome_driver_path))
try:
driver.get(website)
print("Page loaded successfully!🎉")
html = driver.page_source
time.sleep(10)
return html
finally:
driver.quit()
def extract_body_content(html_content):
soup = BeautifulSoup(html_content, "html.parser")
body_content = soup.body
if body_content:
return str(body_content)
return "No content found on the page"
def clean_body_content(body_content):
soup = BeautifulSoup(body_content, "html.parser")
for script_or_style in soup(["script", "style"]):
script_or_style.extract()
cleaned_content = soup.get_text(separator="\n")
clean_content = "\n".join(
line.strip() for line in cleaned_content.splitlines() if line.strip()
)
return clean_content
def split_dom_content(dom_content,max_length=6000):
return [
dom_content[i: i + max_length] for i in range(0,len(dom_content), max_length)
]