-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rwanda_euro_news Webscraping.py
55 lines (40 loc) · 1.52 KB
/
Rwanda_euro_news Webscraping.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
from bs4 import BeautifulSoup
import requests
import pandas as pd
import numpy as np
url = []
name = []
year = []
category = []
page = 1
stop_page = 14
website_page = 'https://www.euronews.com/search?query=Rwanda'
while True:
html_text = requests.get(website_page).text
soup = BeautifulSoup(html_text, 'lxml')
news = soup.find('div', class_='o-block-listing__articles')
articles = news.find_all('article')
for article in articles:
article_name = article.find('a', rel='bookmark').text.strip()
name.append(article_name)
article_url = article.find('a', rel='bookmark')['href']
link = 'https://www.euronews.com' + article_url
url.append(link)
dates = article.find('time').text.strip()
dates = dates.split('/')
date = int(dates[-1])
year.append(date)
article_category = article.find('a', {'data-event': 'article-label'})
article_category = article_category.find('span').text.strip()
category.append(article_category)
print(f'Page {page} finished \n')
page += 1
next_page = soup.find('a', class_='c-paginator__text c-next')
if next_page is not None:
website_page = 'https://www.euronews.com' + next_page['href']
else:
break
news_dict = {'Name': name, 'Category': category, 'Year': year, 'URL': url}
df = pd.DataFrame(news_dict)
print(df)
# df.to_csv(r'C:\Users\HP\PycharmProjects\pythonProject' + '\\Rwanda_Euro_News.csv', index=False)