Spaces:
Sleeping
Sleeping
File size: 1,917 Bytes
bc12604 868467a bc12604 4f473a5 bc12604 d9f2c79 bc12604 d9f2c79 bc12604 868467a bc12604 868467a bc12604 |
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 |
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import FirefoxOptions
import re
import logging
import os
def get_text(url, n_words=15):
try:
driver = None
logging.warning(f"Initiated Scraping {url}")
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
opts = FirefoxOptions()
opts.add_argument("--headless")
opts.add_argument(f"user-agent={user_agent}")
driver = webdriver.Firefox(options=opts)
driver.set_page_load_timeout(30)
driver.get(url)
elem = driver.find_element(By.TAG_NAME, "body").text
sents = elem.split("\n")
sentence_list = []
for sent in sents:
sent = sent.strip()
if (len(sent.split()) >= n_words) and (len(re.findall(r"^\w.+[^\w\)\s]$", sent))>0):
sentence_list.append(sent)
driver.close()
driver.quit()
logging.warning("Closed Webdriver")
logging.warning("Successfully scraped text")
if len(sentence_list) < 3:
raise Exception("Found nothing to scrape.")
return "\n".join(sentence_list), ""
except Exception as e:
logging.warning(str(e))
if driver:
driver.close()
driver.quit()
logging.warning("Closed Webdriver")
err_msg = str(e).split('\n')[0]
return "", err_msg
def scrape_text(url, n_words=15,max_retries=2):
scraped_text = ""
scrape_error = ""
try:
n_tries = 1
while (n_tries <= max_retries) and (scraped_text == ""):
scraped_text, scrape_error = get_text(url=url, n_words=n_words)
n_tries += 1
return scraped_text, scrape_error
except Exception as e:
err_msg = str(e).split('\n')[0]
return "", err_msg
|