diff --git a/scripts/YCombinator News Ranking App/README.md b/scripts/YCombinator News Ranking App/README.md new file mode 100644 index 0000000..74ae8cf --- /dev/null +++ b/scripts/YCombinator News Ranking App/README.md @@ -0,0 +1,8 @@ +# YCombinator News Ranking App +This simple script will scrape the YCombinator News Website and rank the highest upvote news. + +## Usage + +* requires beautifulsoup, requests +* Use `pip install beautifulsoup requests` +* Run `python main.py` diff --git a/scripts/YCombinator News Ranking App/main.py b/scripts/YCombinator News Ranking App/main.py new file mode 100644 index 0000000..5328d50 --- /dev/null +++ b/scripts/YCombinator News Ranking App/main.py @@ -0,0 +1,32 @@ +from bs4 import BeautifulSoup +import requests + +response = requests.get("https://news.ycombinator.com/news") + +yc_webpage = response.text + +soup = BeautifulSoup(yc_webpage, "html.parser") + +articles=[] +articles_span = soup.find_all(name="span", class_="titleline") +for item in articles_span: + articles.append(item.find(name='a')) + +article_texts = [] +article_links = [] + + +for article_tag in articles: + text = article_tag.getText() + article_texts.append(text) + link = article_tag.get("href") + article_links.append(link) +article_upvotes = [int(score.getText().split()[0]) for score in soup.find_all(name="span", class_="score")] + +largest = max(article_upvotes) +largest_index = article_upvotes.index(largest) + +print("Highest Ranked news for today is :\n") +print(f"Upvotes: {largest}") +print(f"Article Heading: {article_texts[largest_index]}") +print(f"Article Link: {article_links[largest_index]}")