-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatest_news.py
42 lines (38 loc) · 1.17 KB
/
latest_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
"""
THIS CODE ON RUNNING WILL PULL LATEST NEWS FROM "TIMES OF INDIA" AND WILL LIST THEM ALONG WITH SHORT DESCRIPTION
"""
import requests,json
from bs4 import BeautifulSoup
website='https://timesofindia.indiatimes.com/news'
response=requests.get(website)
page=response.content
news_html=BeautifulSoup(page,'html.parser')
news=news_html.find('div',attrs={'id':"ulItemContainer"})
news_list=news.findAll('li')
j=1
for i in news_list:
try:
title=i.a['title']
href=i.a['href']
link="https://timesofindia.indiatimes.com"+href
response=requests.get(link)
subpage_html=response.content
news_subpage=BeautifulSoup(subpage_html,'html.parser')
except Exception:
continue
try:
print('\n',j,' -> ',end=' ')
print(title,'\n')
temp=news_subpage.find('script',attrs={'type':'application/ld+json'})
except Exception:
continue
try:
jsontemp=json.loads(temp.text)
for q in jsontemp['speakable']['cssSelector']:
print(q,end=' ')
print()
except Exception:
print("No Description")
j=j+1
if(j>=11):
break