Added a new feature - YCombinator News Ranking Script

This commit is contained in:
Raihan Khan 2022-10-09 03:03:34 +05:30
parent ffd520e3e8
commit 678a154413
2 changed files with 40 additions and 0 deletions

View File

@ -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`

View File

@ -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]}")