-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNews_Headlines.py
More file actions
46 lines (35 loc) · 1.65 KB
/
News_Headlines.py
File metadata and controls
46 lines (35 loc) · 1.65 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
import requests
from bs4 import BeautifulSoup
import schedule
import time
# Function to fetch news from a website
def fetch_news():
# url = "https://www.livemint.com/latest-news" # Replace with the desired news site's URL
url = "https://www.thehindu.com/news/national" # Not working
# url = "https://timesofindia.indiatimes.com/home/headlines" # Not working
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers)
# response = requests.get(url, headers=headers, allow_redirects=False)
# s = requests.Session()
# s.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'
# response = s.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, "html.parser")
# Example: Modify this to match the site's HTML structure
# news_headlines = soup.find_all("h2", class_="headline", limit=10)
news_headlines = soup.find_all("h1", class_="headline", limit=10)
print("Top News Headlines:")
for index, headline in enumerate(news_headlines, 1):
title = headline.text.strip()
link = headline.find("a")["href"]
print(f"{index}. {title}")
print(f"Link: {link}\n")
else:
print(f"Failed to fetch news. HTTP Status Code: {response.status_code}")
# Schedule the function to run daily
# schedule.every().day.at("09:00").do(fetch_news) # Adjust the time as needed
# print("News fetching script is running... Press Ctrl+C to exit.")
# while True:
# schedule.run_pending()
# time.sleep(1)
fetch_news()