-
Notifications
You must be signed in to change notification settings - Fork 5
/
NewsExtraction.py
60 lines (51 loc) · 2.08 KB
/
NewsExtraction.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
56
57
58
59
60
from GoogleNews import GoogleNews
import pandas as pd
import datetime as dt
from newspaper import Article
from newspaper import Config
def Extraction(name):
today = dt.date.today()
today = today.strftime('%m-%d-%Y')
yesterday = dt.date.today() - dt.timedelta(days=1)
yesterday = yesterday.strftime('%m-%d-%Y')
# We need config because sometimes the newspaper package shows errors in downloading the articles
# We use the agent variable to get authorized
agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
config = Config()
config.browser_agent = agent
config.request_timeout = 10
if name != "":
googlenews = GoogleNews(start=yesterday, end=today)
googlenews.search(name) # searching for the news available on the company name
results = googlenews.results() # storing the fetched articles in results
df = pd.DataFrame(results) # creating the dataset
# print(df.head())
# print(df.info())
else:
print("Please enter a valid company name")
try:
list = [] # creating an empty list
for i in df.index:
dict = {}
article = Article(df['link'][i], config=config)
try:
article.download() # downloading the article
article.parse() # parsing the article
article.nlp() # performing natural language processing (nlp)
except:
pass
# storing the data in our dictionary
dict['Date'] = df['date'][i]
dict['Title'] = article.title
dict['Article'] = article.text
dict['Summary'] = article.summary
list.append(dict)
check_empty = not any(list)
# print(check_empty)
if check_empty == False:
news_df = pd.DataFrame(list) # creating dataframe
return news_df
except Exception as e:
# exception handling
print("exception occurred:" + str(e))
return 'Some error occured, please try again with a different name'