Mar-17-2024, 10:53 PM
(This post was last modified: Mar-17-2024, 10:53 PM by SmallCoder14.)
Hi,
So I'm trying to code a script that uses requests and bs4 to go to https://news.ycombinator.com/news, and print out the most number of upvotes. I have a list of all the upvotes, but when a call print(articles_upvotes.sort()) it just prints None. Some thing happens if I save it to a variable and print the new variable. Here's the code:
So I'm trying to code a script that uses requests and bs4 to go to https://news.ycombinator.com/news, and print out the most number of upvotes. I have a list of all the upvotes, but when a call print(articles_upvotes.sort()) it just prints None. Some thing happens if I save it to a variable and print the new variable. Here's the code:
from bs4 import BeautifulSoup
import requests
yc_web_page = requests.get("https://news.ycombinator.com/news").text
yc_soup = BeautifulSoup(yc_web_page, "html.parser")
articles = yc_soup.find_all(name="tr", class_="athing")
articles_text = []
articles_links = []
articles_upvotes = [score.getText().split()[0] for score in yc_soup.find_all(name="td", class_="subtext")]
articles_upvotes = [int(score) for score in articles_upvotes]
for article_tag in articles:
article_text = article_tag.getText()
article_link = article_tag.select("a")[0].get("href")
# article_upvote = yc_soup.find_all(name="td", class_="subtext")[0].select("span.score")[0].text.split()[0]
# print(article_upvote)
articles_text.append(article_text)
articles_links.append(articles_links)
# articles_upvotes.append(article_upvote)
print(articles_text)
print(articles_links)
print(articles_upvotes.sort())
