-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
56 lines (43 loc) · 1.61 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from selenium.webdriver import Remote, ChromeOptions
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from bs4 import BeautifulSoup
from dotenv import load_dotenv
import os
load_dotenv()
WEBDRIVER_URL = os.getenv("WEBDRIVER_URL")
def fetch_web_page(url):
print("Establishing connection with browser service...")
connection = ChromiumRemoteConnection(WEBDRIVER_URL, "goog", "chrome")
with Remote(connection, options=ChromeOptions()) as browser:
browser.get(url)
print("Handling captcha...")
captcha_result = browser.execute(
"executeCdpCommand",
{
"cmd": "Captcha.waitForSolve",
"params": {"detectTimeout": 10000},
},
)
print("Captcha status:", captcha_result["value"]["status"])
print("Page loaded. Extracting data...")
page_html = browser.page_source
return page_html
def parse_html_to_body(html_data):
parser = BeautifulSoup(html_data, "html.parser")
page_body = parser.body
if page_body:
return str(page_body)
return ""
def refine_html_content(raw_html):
parser = BeautifulSoup(raw_html, "html.parser")
for unwanted_tag in parser(["script", "style"]):
unwanted_tag.extract()
refined_text = parser.get_text(separator="\n")
refined_text = "\n".join(
line.strip() for line in refined_text.splitlines() if line.strip()
)
return refined_text
def segment_content(content, segment_size=6000):
return [
content[i : i + segment_size] for i in range(0, len(content), segment_size)
]