File size: 3,699 Bytes
ce2c98f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import pandas as pd
import requests
import time

from bs4 import BeautifulSoup

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


# ======================================
# CREATE DRIVER
# ======================================

def create_driver():

    chrome_options = Options()

    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--disable-gpu")

    chrome_options.binary_location = "/usr/bin/chromium"

    service = Service("/usr/bin/chromedriver")

    driver = webdriver.Chrome(
        service=service,
        options=chrome_options
    )

    return driver


# ======================================
# STATIC SCRAPER
# ======================================

def extract_static(url):

    try:

        headers = {
            "User-Agent": (
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                "AppleWebKit/537.36 (KHTML, like Gecko) "
                "Chrome/124.0.0.0 Safari/537.36"
            )
        }

        response = requests.get(
            url,
            headers=headers,
            timeout=15
        )

        soup = BeautifulSoup(
            response.text,
            "html.parser"
        )

        headline = ""

        if soup.title:
            headline = soup.title.text.strip()

        paragraphs = []

        for p in soup.find_all("p"):

            text = p.get_text(" ", strip=True)

            if len(text) > 40:
                paragraphs.append(text)

        article = "\n".join(paragraphs)

        if len(article) > 100:
            return headline, article

        return None, None

    except:
        return None, None


# ======================================
# SELENIUM SCRAPER
# ======================================

def extract_selenium(driver, url):

    try:

        driver.get(url)

        time.sleep(3)

        soup = BeautifulSoup(
            driver.page_source,
            "html.parser"
        )

        headline = ""

        h1 = soup.find("h1")

        if h1:
            headline = h1.text.strip()

        paragraphs = []

        for p in soup.find_all("p"):

            text = p.get_text(" ", strip=True)

            if len(text) > 40:
                paragraphs.append(text)

        article = "\n".join(paragraphs)

        if len(article) > 100:
            return headline, article

        return None, None

    except:
        return None, None


# ======================================
# MAIN SCRAPER
# ======================================

def scrape_news(file_path):

    df = pd.read_excel(file_path)

    links = df["Link"].dropna().tolist()

    driver = create_driver()

    output_file = "scraped_news.txt"

    with open(output_file, "w", encoding="utf-8") as f:

        for idx, link in enumerate(links, start=1):

            headline, article = extract_static(link)

            if not headline or not article:

                headline, article = extract_selenium(
                    driver,
                    link
                )

            if headline and article:

                f.write("\n")
                f.write("=" * 100)
                f.write("\n")

                f.write(f"ARTICLE {idx}\n")

                f.write("=" * 100)
                f.write("\n\n")

                f.write(f"TITLE:\n{headline}\n\n")

                f.write(f"URL:\n{link}\n\n")

                f.write("CONTENT:\n\n")

                f.write(article)

                f.write("\n\n")

    driver.quit()

    return output_file