Amazing-Python-Scripts
94 строки · 2.3 Кб
1from bs4 import BeautifulSoup
2import requests
3import csv
4
5URL = "https://www.indiatoday.in/"
6
7
8def writeToCSV(topTenNews, category):
9with open("topTen" + category + "News.csv", "w") as file:
10writer = csv.writer(file)
11writer.writerow(["Date", "Link", "Headline"])
12for news in topTenNews:
13writer.writerow(
14[news[2], "https://www.indiatoday.in/" + news[1], news[0]])
15
16
17def getTopTenFromDivTag(category):
18topTenNews = []
19count = 0
20category_url = URL + category
21
22page = requests.get(category_url)
23soup = BeautifulSoup(page.text, "html.parser")
24
25all_div_tags = soup.find_all(class_="detail")
26
27for div in all_div_tags:
28count += 1
29if count > 10:
30break
31headline = div.find("h2").text
32link = div.find("a").attrs["href"]
33date = div.find("a").attrs["href"][-10:]
34topTenNews.append([headline, link, date])
35
36return topTenNews
37
38
39def getTopTenFromLiTag(category):
40topTenNews = []
41count = 0
42category_url = URL + category
43
44page = requests.get(category_url)
45soup = BeautifulSoup(page.text, "html.parser")
46
47ul_tag = soup.find_all(class_="itg-listing")
48ul_tag = str(ul_tag)[25:-6]
49li_tags = ul_tag.split("</li>")
50
51for li in li_tags:
52count += 1
53if count > 10:
54break
55ele = li.split(">")
56link = ele[1].split("=")[1][2:-1]
57headline = ele[2][:-3]
58date = link[-10:]
59topTenNews.append([headline, link, date])
60
61return topTenNews
62
63
64def main():
65
66categories = ["india", "world", "cities", "business", "health", "technology", "sports",
67"education", "lifestyle"]
68
69print("Please Choose a Category from the following list")
70
71for index, category in enumerate(categories):
72print(str(index + 1) + ". " + category.capitalize())
73
74print("Example: Enter 'world' for top 10 world news")
75print()
76
77category = input()
78category = category.lower()
79
80if category not in categories:
81print("\nPlease choose a valid category!")
82exit()
83
84if category in categories[:5]:
85topTenNews = getTopTenFromDivTag(category)
86else:
87topTenNews = getTopTenFromLiTag(category)
88
89writeToCSV(topTenNews, category)
90
91print("Created CSV File Successfully!")
92
93
94main()
95