Created
March 7, 2024 17:51
-
-
Save fsndzomga/6b8c72b404f3b709dd0597e6f97a0f74 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 requests | |
from bs4 import BeautifulSoup | |
from dotenv import load_dotenv | |
import os | |
load_dotenv() | |
BASE_URL = 'https://news.ycombinator.com' | |
USERNAME = os.getenv("USERNAME") | |
PASSWORD = os.getenv("PASSWORD") | |
s = requests.Session() | |
data = {"goto": "news", "acct": USERNAME, "pw": PASSWORD} | |
r = s.post(f'{BASE_URL}/login', data=data) | |
soup = BeautifulSoup(r.text, 'html.parser') | |
if soup.find(id='logout'): | |
print('Successfully logged in') | |
else: | |
print('Authentication Error') | |
r = requests.get('https://news.ycombinator.com') | |
soup = BeautifulSoup(r.text, 'html.parser') | |
links = soup.findAll('tr', class_='athing') | |
formatted_links = [] | |
for link in links: | |
data = { | |
'id': link['id'], | |
'title': link.find_all('td')[2].a.text, | |
"url": link.find_all('td')[2].a['href'], | |
"rank": int(link.find_all('td')[0].span.text.replace('.', '')) | |
} | |
formatted_links.append(data) | |
print(formatted_links) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment