Update scrape.py
Browse files
scrape.py
CHANGED
|
@@ -1,38 +1,21 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
from bs4 import BeautifulSoup
|
| 6 |
-
import time
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
options = Options()
|
| 11 |
-
options.add_argument("--headless")
|
| 12 |
-
options.add_argument("--no-sandbox")
|
| 13 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 14 |
-
|
| 15 |
-
# Use WebDriverManager to automatically download and install the correct version
|
| 16 |
-
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
|
| 17 |
|
| 18 |
def scrape_website(website):
|
| 19 |
-
print("
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
print("CAPTCHA solved or not present. Scraping page content...")
|
| 31 |
-
html = driver.page_source
|
| 32 |
-
return html
|
| 33 |
-
|
| 34 |
-
finally:
|
| 35 |
-
driver.quit()
|
| 36 |
|
| 37 |
def extract_body_content(html_content):
|
| 38 |
soup = BeautifulSoup(html_content, "html.parser")
|
|
@@ -44,11 +27,11 @@ def extract_body_content(html_content):
|
|
| 44 |
def clean_body_content(body_content):
|
| 45 |
soup = BeautifulSoup(body_content, "html.parser")
|
| 46 |
|
| 47 |
-
# Remove
|
| 48 |
for script_or_style in soup(["script", "style"]):
|
| 49 |
script_or_style.extract()
|
| 50 |
|
| 51 |
-
# Extract and clean
|
| 52 |
cleaned_content = soup.get_text(separator="\n")
|
| 53 |
cleaned_content = "\n".join(
|
| 54 |
line.strip() for line in cleaned_content.splitlines() if line.strip()
|
|
@@ -57,7 +40,6 @@ def clean_body_content(body_content):
|
|
| 57 |
return cleaned_content
|
| 58 |
|
| 59 |
def split_dom_content(dom_content, max_length=6000):
|
| 60 |
-
# Split the content into chunks of max_length characters
|
| 61 |
return [
|
| 62 |
dom_content[i : i + max_length] for i in range(0, len(dom_content), max_length)
|
| 63 |
]
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def scrape_website(website):
|
| 9 |
+
print(f"Fetching website content from {website}...")
|
| 10 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 11 |
+
response = requests.get(website, headers=headers)
|
| 12 |
+
|
| 13 |
+
if response.status_code == 200:
|
| 14 |
+
print("Website content fetched successfully!")
|
| 15 |
+
return response.text
|
| 16 |
+
else:
|
| 17 |
+
print(f"Failed to fetch website: Status code {response.status_code}")
|
| 18 |
+
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def extract_body_content(html_content):
|
| 21 |
soup = BeautifulSoup(html_content, "html.parser")
|
|
|
|
| 27 |
def clean_body_content(body_content):
|
| 28 |
soup = BeautifulSoup(body_content, "html.parser")
|
| 29 |
|
| 30 |
+
# Remove script and style elements
|
| 31 |
for script_or_style in soup(["script", "style"]):
|
| 32 |
script_or_style.extract()
|
| 33 |
|
| 34 |
+
# Extract text and clean up the content
|
| 35 |
cleaned_content = soup.get_text(separator="\n")
|
| 36 |
cleaned_content = "\n".join(
|
| 37 |
line.strip() for line in cleaned_content.splitlines() if line.strip()
|
|
|
|
| 40 |
return cleaned_content
|
| 41 |
|
| 42 |
def split_dom_content(dom_content, max_length=6000):
|
|
|
|
| 43 |
return [
|
| 44 |
dom_content[i : i + max_length] for i in range(0, len(dom_content), max_length)
|
| 45 |
]
|