mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-30 15:31:10 +00:00
Added a new feature - YCombinator News Ranking Script
This commit is contained in:
parent
ffd520e3e8
commit
678a154413
8
scripts/YCombinator News Ranking App/README.md
Normal file
8
scripts/YCombinator News Ranking App/README.md
Normal 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`
|
32
scripts/YCombinator News Ranking App/main.py
Normal file
32
scripts/YCombinator News Ranking App/main.py
Normal 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]}")
|
Loading…
Reference in New Issue
Block a user