-
Notifications
You must be signed in to change notification settings - Fork 0
/
News.py
55 lines (43 loc) · 1.98 KB
/
News.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
import newspaper
from newspaper import news_pool
import textwrap
class News:
def __init__(self):
self.base_url = "https://tvn24.pl/tagi/Koronawirus_w_Polsce"
self.newspaper_link = "https://www.medicalnewstoday.com/"
def get_who_articles(self):
covid_articles = newspaper.build(self.newspaper_link, memoize_articles=False)
papers = [covid_articles, ]
news_pool.set(papers, threads_per_source=4)
news_pool.join()
for index, article in enumerate(covid_articles.articles):
print(article.url)
article.parse()
write_file = open('sites/articles/article' + str(index) + '.txt', 'w', encoding='utf-8')
write_file.write(str(article.title) + "\n")
write_file.write(textwrap.fill(article.text, width=120))
write_file.close()
def get_gov_articles(self):
gov_articles = newspaper.build(self.base_url, memoize_articles=False)
for index, article in enumerate(gov_articles.articles):
if index < 15:
article.download()
article.parse()
write_file = open('sites/gov_articles/article' + str(index+1) + '.txt', 'w', encoding='utf-8')
write_file.write(str(article.title) + "\n")
write_file.write(textwrap.fill(article.text, width=120))
write_file.close()
else:
break
def get_my_articles(self, url):
gov_articles = newspaper.build(url, memoize_articles=False)
for index, article in enumerate(gov_articles.articles):
if index < 15:
article.download()
article.parse()
write_file = open('sites/my_articles/article' + str(index+1) + '.txt', 'w', encoding='utf-8')
write_file.write(str(article.title) + "\n")
write_file.write(textwrap.fill(article.text, width=120))
write_file.close()
else:
break