|
| 1 | +""" |
| 2 | +a simple terminal program to find new about certain topic by web scraping site. |
| 3 | +site used : |
| 4 | +1. Times of India, |
| 5 | + link : https://timesofindia.indiatimes.com/india/ |
| 6 | +2. India's Today, |
| 7 | + link : https://www.indiatoday.in/topic/ |
| 8 | +""" |
| 9 | + |
| 10 | +import requests |
| 11 | +from bs4 import BeautifulSoup |
| 12 | +import webbrowser |
| 13 | +import time |
| 14 | + |
| 15 | + |
| 16 | +def Times_of_India(userInput, ua): |
| 17 | + bold_start = "\033[1m" |
| 18 | + bold_end = "\033[0m" |
| 19 | + |
| 20 | + url = "https://timesofindia.indiatimes.com/india/" |
| 21 | + url += userInput |
| 22 | + |
| 23 | + res = requests.post(url, headers=ua) |
| 24 | + soup = BeautifulSoup(res.content, "html.parser") |
| 25 | + data = soup.find_all(class_="w_tle") |
| 26 | + |
| 27 | + if len(data) > 0: |
| 28 | + print("News available :", "\N{slightly smiling face}") |
| 29 | + if len(data) == 0: |
| 30 | + return 0 |
| 31 | + |
| 32 | + for item in range(len(data)): |
| 33 | + print(bold_start, "\033[1;32;40m \nNEWS : ", item + 1, bold_end, end=" ") |
| 34 | + data1 = data[item].find("a") |
| 35 | + print(bold_start, data1.get_text(), bold_end) |
| 36 | + |
| 37 | + bol = input("For more details ->(y) (y/n) :: ") |
| 38 | + if bol == "y": |
| 39 | + url += data1.get("href") |
| 40 | + print("%s" % url) |
| 41 | + |
| 42 | + webbrowser.open(url) |
| 43 | + |
| 44 | + return len(data) |
| 45 | + |
| 46 | + |
| 47 | +def india_today(userInput, ua): |
| 48 | + bold_start = "\033[1m" |
| 49 | + bold_end = "\033[0m" |
| 50 | + |
| 51 | + url = "https://www.indiatoday.in/topic/" |
| 52 | + url += userInput |
| 53 | + |
| 54 | + res = requests.get(url, headers=ua) |
| 55 | + soup = BeautifulSoup(res.content, "html.parser") |
| 56 | + data = soup.find_all(class_="field-content") |
| 57 | + |
| 58 | + if len(data) > 0: |
| 59 | + print("\nNews available : ", "\N{slightly smiling face}") |
| 60 | + k = 0 |
| 61 | + for i in range(len(data)): |
| 62 | + data1 = data[i].find_all("a") |
| 63 | + for j in range(len(data1)): |
| 64 | + print(bold_start, "\033[1;32;40m\nNEWS ", k + 1, bold_end, end=" : ") |
| 65 | + k += 1 |
| 66 | + print(bold_start, data1[j].get_text(), bold_end) |
| 67 | + bol = input("\nFor more details ->(y) (y/n) :: ") |
| 68 | + if bol == "y" or bol == "Y": |
| 69 | + data2 = data[i].find("a") |
| 70 | + url = data2.get("href") |
| 71 | + webbrowser.open(url) |
| 72 | + |
| 73 | + return len(data) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + |
| 78 | + bold_start = "\033[1m" |
| 79 | + bold_end = "\033[0m" |
| 80 | + print("\033[5;31;40m") |
| 81 | + print( |
| 82 | + bold_start, |
| 83 | + " HERE YOU WILL GET ALL THE NEWS JUST IN ONE SEARCH ", |
| 84 | + bold_end, |
| 85 | + ) |
| 86 | + print("\n") |
| 87 | + localtime = time.asctime(time.localtime(time.time())) |
| 88 | + print(bold_start, localtime, bold_end) |
| 89 | + |
| 90 | + ua = { |
| 91 | + "UserAgent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0" |
| 92 | + } |
| 93 | + print( |
| 94 | + bold_start, |
| 95 | + "\n\033[1;35;40m Search any news (state , city ,Country , AnyThings etc) : ", |
| 96 | + bold_end, |
| 97 | + end=" ", |
| 98 | + ) |
| 99 | + |
| 100 | + userInput = input() |
| 101 | + |
| 102 | + print(bold_start, "\033[1;33;40m \n") |
| 103 | + print("Which news channel data would you prefer") |
| 104 | + print("1. Times of india") |
| 105 | + print("2. India's Today", bold_end) |
| 106 | + |
| 107 | + say = int(input()) |
| 108 | + |
| 109 | + if say == 1: |
| 110 | + length = Times_of_India(userInput, ua) |
| 111 | + if length == 0: |
| 112 | + print("Sorry Here No News Available", "\N{expressionless face}") |
| 113 | + print("\n") |
| 114 | + print( |
| 115 | + "Would you like to go for India's Today (y/n):: ", |
| 116 | + "\N{thinking face}", |
| 117 | + end=" ", |
| 118 | + ) |
| 119 | + speak = input() |
| 120 | + if speak == "y": |
| 121 | + length = india_today(userInput, ua) |
| 122 | + if length == 0: |
| 123 | + print("Sorry No news", "\N{expressionless face}") |
| 124 | + else: |
| 125 | + print("\nThank you", "\U0001f600") |
| 126 | + else: |
| 127 | + print("\nThank you", "\U0001f600") |
| 128 | + elif say == 2: |
| 129 | + length = india_today(userInput, ua) |
| 130 | + |
| 131 | + if length == 0: |
| 132 | + print("Sorry No news") |
| 133 | + else: |
| 134 | + print("\nThank you", "\U0001f600") |
| 135 | + else: |
| 136 | + print("Sorry", "\N{expressionless face}") |
0 commit comments