Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Eggy-Chat-Bot-/HotNews.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
157 lines (144 sloc)
9.84 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import site # importing site module so we can use site specifc paths etc. | |
import os # importing module os so we can use os features and functions such as clearing terminal | |
site.getsitepackages() # function Returning a list containing all global site-packages directories | |
import requests # import requests is brining the requests module which allows to use simple HTTP Functions | |
from bs4 import BeautifulSoup # importing beatifulsoup from bs4 module which is very usefull for scraping websites and helped me a lot | |
from datetime import datetime # importing the datetime module from date and time | |
# from GlobalVariables import testname # code which wasn't used used to import name from globalvairables | |
URL = "https://www.bbc.com/news" # setting 'URL' as the string to the bbc/news website | |
page = requests.get(URL) # sets 'page' with information from the acutal website as requests.get grabs infromation # from URL which is bbc | |
soup = BeautifulSoup(page.content, "html.parser") # with this line we are both making a 'soup' and a parser which esecianlly analyses the website we want and sorts it in a way we can acess in html | |
results = soup.find(id="site-container") # soup.find searches for an id in page which is the URL, and the id it is looking for | |
HotNews = results.find_all("div", id="latest-stories-tab-container") # makes list of all "latest stories tab container" used for titles | |
links = results.find_all("a") # makes list of all A in results used for descirptions | |
def news():# main function for getting the news and displaying them | |
LinkUrls = [] # empty list made for links | |
for link in links: # loop to get all links from links variable which holds all "a" tags via html | |
link_url = link["href"] # link_url has the value | |
FullLink = ("bbc.com" + link_url) # make the "full link to the website because links in html are only extensions missing bbc.com | |
LinkUrls.append(FullLink) # add "fulllink" to to the LinkUrls list | |
for Hotstory in HotNews: # loop to use for Hotstory & descirption | |
hotstory2 = Hotstory.find("h3", class_="gs-c-promo-heading__title gel-paragon-bold nw-o-link-split__text") | |
# hotstory2 is the hot story h3 and class if found in html | |
hotstory3 = Hotstory.find_all("h3", class_="gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text") | |
# hotstory3 is a list of all h3 headers with the class as above this is because the stories had all the | |
# same classes and all h3 so to fix this and get the stories i made a list of all of them which i later simply | |
# used in the loop to add the 8 stories i need to the "stories" list after removcing all the html code from | |
# to leave just text | |
descriptionDone = Hotstory.find("p", class_="gs-c-promo-summary gel-long-primer gs-u-mt nw-c-promo-summary") | |
# the same as for hotstory2/3 the but with the "p" insted of the h3 header in html | |
descriptionlist = Hotstory.find_all("p", class_="gs-c-promo-summary gel-long-primer gs-u-mt nw-c-promo-summary") | |
# the same as for hotstory2/3 the but with the "p" insted of the h3 header in html | |
Stories = [] # list of Stories which will be added once only text remaning | |
StoryDescriptions = [] # list of descriptions which will be added once only text reamining | |
c = 0 # counter for the next loop to | |
for x in range(0, 9): #loop for 8 stories and descriptions | |
hottest = "".join(str(hotstory3[c:c + 1])) #takes out certain entry in list hootstory3 and changes it to str | |
# so that we can edit it more easily, using c:c+1 to make sure we only get the first inputs in the list because | |
# we need the closest ones | |
hottest = hottest.replace('<h3 class="gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text">', '') | |
hottest = hottest.replace('</h3>', '') | |
hottest = hottest.translate({ord(']'): None}) | |
hottest = hottest.replace('[', '') | |
#all lines replace parts of the str which are not need to keep just text | |
Description = "".join(str(descriptionlist[c:c + 1])) | |
Description = Description.replace('<p class="gs-c-promo-summary gel-long-primer gs-u-mt nw-c-promo-summary">', '') | |
Description = Description.replace('</p>', '') | |
Description = Description.translate({ord(']'): None}) | |
# all lines replace parts of the str which are not need to keep just text | |
Stories.append(hottest) # add each loop of hottest to the stories list ready for print | |
StoryDescriptions.append(Description) # add each loop of Description to the StoryDescriptions list ready for print | |
c = c + 1 # add c+1 so that the next loop has a different value for line 40&48 | |
# """ Story 1 """ | |
# lines 47 to 50 are example of code which is used now in the loop from line 32 to 44 which prior to this was | |
# all duplicate like below however due to an error with the loop i couldn't fix it however i managed to fix it | |
# and make a single loop and delte 100+ lines | |
#description1 = "".join(str(descriptionlist[2])) | |
#description1 = description1.replace('<p class="gs-c-promo-summary gel-long-primer gs-u-mt nw-c-promo-summary">', '') | |
#description1 = description1.replace('</p>', '') | |
#description1 = description1.translate({ord(']'): None}) | |
while True: # while true so that incase there is a incorrect input it will loop to ask the user again | |
print("**********Todays Biggest News**********") # header | |
print("Welcome to today's hottest news from BBC \nToday is " + datetime.today().strftime('%d-%m-%Y %H:%M:%S')) | |
# header with date and time and weclomig user | |
print("**********Todays Biggest News**********") # header | |
print("") | |
Choice = input("Stroy 1 " + hotstory2.text + "\nStroy 2 " + str(Stories[0]) + "\nStroy 3 " + str(Stories[1]) + "\nStroy 4 " + str(Stories[2]) + "\nStroy 5 " + str(Stories[3]) + "\nStroy 6 " + str(Stories[4]) + "\nStroy 7 " + str(Stories[5]) + "\nStroy 8 " + str(Stories[6]).translate({ord('['): None}) + "\n\nFor more information about a story write the number of the story \nOtherwise if you would like to go back to the main menu type exit\n") | |
# display stories as well as ask user for input, displying stories grabing story from list + the item in list and chaning to str | |
if Choice == "1": # if choice will be 1 run this part of code else continue and check next | |
os.system('cls') #clear terminal | |
print('"' + descriptionDone.text + '"'" \nfor more infromation head too") | |
print(LinkUrls[3:4]) # link from linkurls list check | |
option() | |
elif Choice == "2": | |
os.system('cls') | |
print('"' + str(StoryDescriptions[2]) + '"\n'"\nfor more infromation head too") | |
print(LinkUrls[10]) | |
option() | |
elif Choice == "3": | |
os.system('cls') | |
print('"' + str(StoryDescriptions[3]) + '"\n'"\nfor more infromation head too") | |
print(LinkUrls[12]) | |
option() | |
elif Choice == "4": | |
os.system('cls') | |
print('"' + str(StoryDescriptions[4]) + '"\n'"\nfor more infromation head too") | |
print(LinkUrls[14]) | |
option() | |
elif Choice == "5": | |
os.system('cls') | |
print('"' + str(StoryDescriptions[5]) + '"\n'"\nfor more infromation head too") | |
print(LinkUrls[16]) | |
option() | |
elif Choice == "6": | |
os.system('cls') | |
print('"' + str(StoryDescriptions[6]) + '"\n'"\nfor more infromation head too") | |
print(LinkUrls[19]) | |
option() | |
elif Choice == "7": | |
os.system('cls') | |
print('"' + str(StoryDescriptions[7]) + '"'"\nfor more infromation head too") | |
print(LinkUrls[21]) | |
option() | |
elif Choice == "8": | |
os.system('cls') | |
print('"' + str(StoryDescriptions[8]) + "\nfor more infromation head too") | |
print(LinkUrls[24]) | |
option() | |
elif Choice == "exit" or "Exit": | |
os.system('cls') | |
return () | |
else: | |
print("\n I dont undeserstand please try again ") | |
continue | |
def googlenews(): # un used code not working function due to lack of time | |
url = "https://google-news.p.rapidapi.com/v1/top_headlines" # url for google-news | |
querystring = {"lang": "en", "country": "US"} | |
headers = {'x-rapidapi-host': "google-news.p.rapidapi.com",'x-rapidapi-key': "SIGN-UP-FOR-KEY"} | |
response = requests.request("GET", url, headers=headers, params=querystring) | |
return (response.text) | |
def option(): # fucntion used as an option not to repeat code asking user if they want another story | |
while True: # while true so that it loops if input incorrect | |
x = input("\n1. to go back to main menu, 2. to get another story\n") | |
if x == "1": | |
os.system('cls') #clear terminal | |
menu() | |
elif x == "2": | |
os.system('cls') | |
return | |
else: | |
print("\n I dont undeserstand please try again ") | |
continue | |
def menu(): # main choice menu | |
while True: | |
FirstChoice = input("\nWelcome to Eggy's Hot News Today Is \n:" + datetime.today().strftime('%d-%m-%Y %H:%M:%S' + "\n****Menu*****\n1.Eggy's Hottest News \n2.Google Latest News \n3.to Exit\n")) | |
if FirstChoice == "1": | |
os.system('cls') #clear terminal | |
news() | |
elif FirstChoice == "2": | |
os.system("cls") | |
elif FirstChoice == "3": | |
os.system('cls') | |
else: | |
print("I dont undeserstand please try again") | |
print(menu()) #run and output the function |