forked from sukeesh/Jarvis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewws.py
More file actions
57 lines (49 loc) · 1.5 KB
/
newws.py
File metadata and controls
57 lines (49 loc) · 1.5 KB
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
import webbrowser
import requests
from bs4 import BeautifulSoup
from colorama import init
from colorama import Fore, Back, Style
def show_news():
url = "https://news.google.co.in/"
wp = requests.get(url)
soup = BeautifulSoup(wp.text, "lxml")
title_to_be_shown = soup.findAll("span")
cnt = 1
title_contents = []
parent_urls = []
for i in title_to_be_shown:
if i.get('class')!=None and "titletext" in i.get('class'):
title_contents.append(str(i.get_text()))
parent_urls.append(str(i.parent['url']))
cnt += 1
if cnt == 5:
break
news_contents = []
divs = soup.findAll("div")
cnt = 1
for i in divs:
if not i.get('class') and "esc-lead-snippet-wrapper" in i.get('class'):
news_contents.append(str(i.get_text()))
cnt += 1
if cnt == 5:
break
i = 0
while i < len(title_contents):
print (Fore.GREEN + str(i + 1) + " - " + title_contents[i] + Fore.RESET)
i += 1
print("Type index to expand news\n")
try:
idx = int(raw_input())
except:
idx = int(input())
idx -= 1
print(">> {0} {1} {2}\n".format(Fore.BLUE, news_contents[idx], Fore.RESET))
print("{0}\n Do you want to read more? (yes/no): {1}".format(Fore.RED, Fore.RESET))
try:
take_yn = raw_input()
except:
take_yn = input()
if take_yn.lower() == "yes":
webbrowser.open(parent_urls[idx])
else:
return