-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscraper.py
241 lines (222 loc) · 9.62 KB
/
scraper.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
Authors: Rishabh Gupta (rg089), Vishal Singhania (vishalvvs)
"""
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import re
class IndiaToday():
@staticmethod
def generate_dataset():
data= []
for i in range(3):
url = "https://www.indiatoday.in/top-stories" + "?page=" + str(i)
html = requests.get(url)
soup = BeautifulSoup(html.text,"lxml")
for i in soup.find_all(class_ = "catagory-listing"):
news_url = 'https://www.indiatoday.in/'+i.a["href"]
news_title, news_content, time = IndiaToday().get_content(news_url)
if news_content == None or news_content == "":
continue
article = {}
article["link"] = news_url
article["content"] = news_content
article["source"] = "IT"
article["title"] = news_title
article["time"] = time
data.append(article)
return data
@staticmethod
def get_content(url):
ar = requests.get(url)
article = BeautifulSoup(ar.text,"lxml")
title = article.find("h1").get_text()
try:
time = article.find("span", class_="update-data").text
time = re.sub("Updated:|IST|AM|PM", "", time, flags=re.IGNORECASE).strip()
# time = datetime.strptime(time, "%B %d, %Y %H:%M")
except:
time = None
if (article.find('div',{"class":"description"}) == None) or time==None:
return None, None, None
else:
content = ""
for para in article.find(class_ = "description").find_all("p")[:-3]:
content+=para.get_text()
return title,content,time
class TheHindu():
@staticmethod
def get_content(url):
html = requests.get(url)
article = BeautifulSoup(html.text,"lxml")
content = ""; time = ""
for i in article.find_all("span", class_="blue-color ksl-time-stamp"):
if i.find("none") is not None:
time = i.get_text()
time = re.sub("Updated:|IST|AM|PM", "", time, flags=re.IGNORECASE).strip()
break
# time = datetime.strptime(time, "%B %d, %Y %H:%M")
paras = article.find_all("p")[1:-5]
for para in paras:
content+=para.get_text()
return content, time
@staticmethod
def generate_dataset():
url = "https://www.thehindu.com/trending/"
html = requests.get(url)
soup = BeautifulSoup(html.text,"lxml")
data = []
for story in soup.find_all(class_ = "story-card-news"):
news_title = story.get_text().split("\n")[6]
possible_links = [el.get("href", "") for el in story.find_all("a") if el.get("href", "").endswith(".ece")]
if len(possible_links) == 0: continue
content_url = possible_links[0]
content, time = TheHindu.get_content(content_url)
if content != "":
article = {}
article["link"] = content_url
article["content"] = content
article["source"] = "TH"
article["title"] = news_title
article["time"] = time
data.append(article)
else:
continue
return data
class TimesOfIndiaNews():
@staticmethod
def get_toi_links_titles():
url = "https://timesofindia.indiatimes.com/news"
data = requests.get(url).content
soup = BeautifulSoup(data,"lxml")
pages = len(soup.find("ul", class_ = "curpgcss").find_all("li"))
page_data = [soup] + [BeautifulSoup(requests.get(url+f"/{page_num}").content, "lxml") for page_num in range(2, pages+1)]
links_titles = []
for page in page_data:
ul = page.find('ul', {'class': 'cvs_wdt clearfix', "data-msid": "-2128958273"})
links_titles += [("https://timesofindia.indiatimes.com" + li.find("a").get("href", ""), li.find("a").get("title", "")) for li in ul.find_all("li") if li.find("a").get("href", "").startswith("/")]
return links_titles
@staticmethod
def get_toi_links_titles_old(div_class):
url = "https://timesofindia.indiatimes.com/home/headlines"
data = requests.get(url).content
soup = BeautifulSoup(data,"lxml")
ul = soup.find('div', {'class': div_class}).find('ul', {'class': 'clearfix'})
links_titles = [("https://timesofindia.indiatimes.com" + li.find("a").get("href", ""), li.find("a").get("title", "")) for li in ul.find_all("li") if li.find("a").get("href", "").startswith("/")]
return links_titles
@staticmethod
def get_toi_content(url):
data = requests.get(url).content
soup = BeautifulSoup(data,"lxml")
div = soup.find("div", class_= "_3YYSt clearfix ")
if div is None:
div = soup.find("div", class_= "_3YYSt clearfix")
if div is not None:
time = soup.find("div", class_="yYIu- byline").find("span").text
time = re.sub("Updated:|IST|AM|PM", "", time, flags=re.IGNORECASE).strip()
# time = datetime.strptime(time, "%b %d, %Y, %H:%M")
return "".join(list(div.strings)), time
return None, None
@staticmethod
def generate_dataset():
# link_titles = TimesOfIndiaNews.get_toi_links_titles("headlines-list") + TimesOfIndiaNews.get_toi_links_titles("top-newslist")
link_titles = TimesOfIndiaNews.get_toi_links_titles()
data = []
for link, title in link_titles:
article = {}
content, time = TimesOfIndiaNews.get_toi_content(link)
if content is not None:
article["link"] = link
article["title"] = title
article["source"] = "TOI"
article["content"] = content
article["time"] = time
data.append(article)
return data
class NDTVNEWS():
@staticmethod
def generate_dataset():
data= []
url = "https://www.ndtv.com/top-stories"
html = requests.get(url)
soup = BeautifulSoup(html.text,"lxml")
for i in soup.find_all("h2",{"class" : "newsHdng"}):
news_title = i.get_text().strip()
news_url = i.a["href"]
news_content, time = NDTVNEWS().get_content(news_url)
article = {}
article["link"] = news_url
article["content"] = news_content
article["source"] = "NDTV"
article["title"] = news_title
article["time"] = time
data.append(article)
return data
@staticmethod
def get_content(url):
ar = requests.get(url)
article = BeautifulSoup(ar.text,"lxml")
if article.find('div',{'id':"ins_storybody"}) == None:
return None, None
else:
time = article.find("span", {"itemprop":"dateModified"}).text
time = re.sub("Updated:|IST|AM|PM", "", time, flags=re.IGNORECASE).strip()
# time = datetime.strptime(time, "%B %d, %Y %H:%M")
content = ""
for para in article.find('div',{'id':"ins_storybody"}).find_all("p"):
content+=para.get_text()
return content, time
class TheIndianExpress():
@staticmethod
def generate_dataset():
data= []
url = "https://indianexpress.com/section/india/"
html = requests.get(url)
soup = BeautifulSoup(html.text,"lxml")
for i in soup.find_all(class_ = "title"):
news_title = i.get_text()
news_url = i.a["href"]
news_content, time = TheIndianExpress().get_content(news_url)
if news_content is not None:
article = {}
article["link"] = news_url
article["content"] = news_content.strip()
article["source"] = "TIE"
article["title"] = news_title.strip()
article["time"] = time
data.append(article)
else:
pass
return data
@staticmethod
def get_content(url):
ar = requests.get(url)
article = BeautifulSoup(ar.text,"lxml")
if article.find(id="pcl-full-content") == None:
return None, None
else:
time = article.find("span", {"itemprop":"dateModified"}).text
time = re.sub("Updated:|IST|AM|PM", "", time, flags=re.IGNORECASE).strip()
# time = datetime.strptime(time, "%B %d, %Y %H:%M:%S")
content = ""
for para in article.find('div', id = "pcl-full-content").find_all("p"):
content+=para.get_text()
return content, time
class Data:
@staticmethod
def collect(source="all"):
"""
Input : Ticker of News Site to Scraper, Default : The Indian Express
Output: List of Dictionary of the scraped articles from the source site
"""
d = {"toi":TimesOfIndiaNews,"tie":TheIndianExpress,"th":TheHindu,"it":IndiaToday,"ndtv":NDTVNEWS}
if source in d:
news = d[source].generate_dataset()
return news
elif source == "all":
papers = [IndiaToday, TheHindu, TimesOfIndiaNews, NDTVNEWS, TheIndianExpress]
articles = []
for paper in papers:
articles += paper.generate_dataset()
return articles
return None