-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb_scraping.py
277 lines (198 loc) · 6.56 KB
/
web_scraping.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#web_scraping_prototype
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import os
from time import sleep
from datetime import date,datetime
from bs4 import BeautifulSoup
import bs4
import re
import traceback
class article:
def __init__(self,link,article_age=-1):
self.link = link
self.text = ''
self.article_age = article_age
def set_text(self,text):
self.text = text
def set_upload_time(self,article_age):
self.article_age = article_age
def __repr__(self):
return str(('\nLink: '+self.link+'\nArticle Age: '+str(self.article_age)+'\nText: '+self.text).encode('utf-8'))
def bbc_search(phrase):
articles = []
try:
search = phrase
search = '+'.join(search.split())
url = "https://www.bbc.co.uk/search?q="+search+"&filter=news"
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html5lib')
articles_on_page = soup.find('div',attrs={'class':"css-5qhota-Stack e1y4nx260"})
articles_list = articles_on_page.find_all('div',attrs={'class':"css-16ck4pk-PromoContent ett16tt10"})
article_links = []
for item in articles_list:
try:
time_span = item.find('span',attrs={'class':"css-1hizfh0-MetadataSnippet ecn1o5v0"})
time_text = time_span.find_all('span')[1].text
article_time = datetime.strptime(time_text,'%d %b %Y').timestamp()
now = datetime.today().timestamp()
if now-article_time > 259200:
#print('bbc-skipped')
break
article_link = str(item.find('a',attrs={'class':"css-rjlb9k-PromoLink ett16tt7"}).attrs['href'])
if not (re.search(r'\bprogrammes\b',article_link)):
temp_article = article(article_link,now-article_time)
articles.append(temp_article)
except Exception as e:
print(traceback.format_exc())
except Exception as e:
print(traceback.format_exc())
return []
return articles
def bbc_webpage_to_text(link):
try:
page = requests.get(link)
#print(page.text[:1000])
soup = BeautifulSoup(page.text, 'html5lib')
body_content = soup.find('div',attrs={'class':"story-body__inner"})
sentence_list = body_content.find_all('p')
text = []
for sentence in sentence_list:
text.append(sentence.text)
text= ''.join(text)
except Exception as e:
print(traceback.format_exc())
# print(e)
return ""
return text
def start_browser():
dir = os.path.dirname(__file__)
chrome_driver_path = dir + "/chromedriver.exe"
chrome_options = Options()
# "https://www.aljazeera.com/Search/?q="+search
chrome_options.add_argument("--headless")
#chrome_options.binary_location = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'
# create a new Chrome session
return webdriver.Chrome(options=chrome_options)
def aljazeera_search(phrase):
driver = start_browser()
search = phrase
search = '+'.join(search.split())
driver.get("https://www.aljazeera.com/Search/?q="+search)
#https://www.aljazeera.com/Search/?q=trump
sleep(20)
articles_list = driver.find_elements_by_xpath("//div[@class='row topics-sec-item ']")
article_links = []
articles = []
for item in articles_list:
# print(article.text)
try:
exact_article = item.find_element_by_xpath("div[@class='col-sm-7 topics-sec-item-cont']")
article_link = exact_article.find_element_by_xpath("a")
article_time = exact_article.find_element_by_xpath("//span[@class='humanize-datetime']").get_attribute('data-modifieddate')[:-1]
article_time = datetime.strptime(article_time,'%Y-%m-%dT%H:%M:%S').timestamp()
now = datetime.today().timestamp()
if now-article_time > 259200:
#print("skipped")
continue
if phrase in article_link.text.lower() and "in pictures" not in article_link.text.lower():
temp_article = article(article_link.get_attribute('href'),now-article_time)
articles.append(temp_article)
except Exception as e:
print(traceback.format_exc())
continue
driver.close()
return articles
def aljazeera_webpage_to_text(link):
#print('a')
page = requests.get(link)
#print(page.text[:1000])
soup = BeautifulSoup(page.text, 'html5lib')
body_content = soup.find('div',attrs={'class':"main-article-body"})
sentence_list = body_content.find_all('p')[:-2]
article = ""
for sentence in sentence_list:
text=""
text = sentence.text
article += text
return str(article)
def toi_search(phrase):
#while True:
#search = input("Enter an item to search: ")
search = phrase
search = '+'.join(search.split())
url = "https://timesofindia.indiatimes.com/topic/"+search+"/news"
#print(url)
page = requests.get(url)
#print(page.text[:1000])
soup = BeautifulSoup(page.text, 'html5lib')
list_of_articles = soup.find_all('li',attrs={'class':"article"})
links =[]
for item in list_of_articles:
link = item.find('a')
article_time = link.find_all('span')[4]['rodate']
article_time = datetime.strptime(article_time,'%Y-%m-%dT%H:%M:%SZ').timestamp()
now = datetime.today().timestamp()
if now-article_time > 259200:
print('skipped')
break
links.append("https://timesofindia.indiatimes.com"+link['href'])
return links
# toi_webpage_to_text(links[0])
def toi_webpage_to_text(link):
page = requests.get(link)
#print(page.text[:1000])
soup = BeautifulSoup(page.text, 'html5lib')
sentence_list = []
text = []
try:
body_content = soup.find('div',attrs={'class':"Normal"})
start_end = [body_content.contents[0],body_content.contents[-1]]
sentence_list = body_content.find_all('p')
if len(sentence_list)==0:
raise Exception
for sentence in sentence_list:
#print(sentence)
if isinstance(sentence.contents[0],bs4.element.NavigableString):
text.append(sentence.contents[0])
except Exception as e:
#print(e)
return
# print(text)
try:
text= start_end[0]+''.join(text)+start_end[1]
if len(text)>200:
print(text,"\n\n\n\n\n")
# print()
except Exception as e:
return 'Error'
return text
def get_articles(trend):
articles = []
bbc_articles = bbc_search(trend)
for article in bbc_articles:
text = bbc_webpage_to_text(article.link)
article.set_text(bbc_webpage_to_text(article.link))
for article in bbc_articles:
if len(article.text)!=0:
#print(article.link)
#print(len(article.text))
articles.append(article)
aljazeera_articles = aljazeera_search(trend)
for article in aljazeera_articles:
text = aljazeera_webpage_to_text(article.link)
if len(text)==0:
del article
else:
article.set_text(text)
for article in aljazeera_articles:
if len(article.text)!=0:
#print(article)
#print(len(article.text))
articles.append(article)
return articles
if __name__ == '__main__':
for article in get_articles('boris'):
print(article)