-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_bbc.py
34 lines (26 loc) · 1.15 KB
/
scrape_bbc.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
import requests
from bs4 import BeautifulSoup
def fetch_news_bbc():
URL = 'https://www.bbc.com/news'
response = requests.get(URL)
soup = BeautifulSoup(response.content, 'html.parser')
containers = soup.find_all('div', class_='gs-c-promo-body', limit=15)
news_data = []
seen_headlines = set()
for container in containers:
headline_elem = container.find('h3', class_='gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text')
# description_elem = container.find('p', class_='gs-c-promo-summary gel-long-primer gs-u-mt nw-c-promo-summary')
if headline_elem:
headline_text = headline_elem.text.strip()
# description_text = description_elem.text.strip()
if headline_text not in seen_headlines and len(news_data) < 10:
seen_headlines.add(headline_text)
news_data.append({
'headline': headline_text,
# 'description': description_text
})
return [item['headline'] for item in news_data]
# news = fetch_news_bbc()
# for item in news:
# print(item['headline'])
# print("---")