-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
45 lines (36 loc) · 1.02 KB
/
scraper.py
File metadata and controls
45 lines (36 loc) · 1.02 KB
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
import requests
from bs4 import BeautifulSoup
def scrape_bbc():
url = "https://www.bbc.com/news"
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
headlines = soup.select("h3")
result = []
for h in headlines[:10]:
text = h.get_text(strip=True)
if text:
result.append(text)
return result
def scrape_guardian():
url = "https://www.theguardian.com/uk"
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
headlines = soup.select("h3")
result = []
for h in headlines[:10]:
text = h.get_text(strip=True)
if text:
result.append(text)
return result
def scrape_all():
print("Fetching BBC...")
bbc_news = scrape_bbc()
print("Fetching Guardian...")
guardian_news = scrape_guardian()
print("\n=== BBC Top Headlines ===")
for item in bbc_news:
print("-", item)
print("\n=== Guardian Top Headlines ===")
for item in guardian_news:
print("-", item)
scrape_all()