Skip to content

Instantly share code, notes, and snippets.

@fsndzomga
Created March 7, 2024 17:51
Show Gist options
  • Save fsndzomga/6b8c72b404f3b709dd0597e6f97a0f74 to your computer and use it in GitHub Desktop.
Save fsndzomga/6b8c72b404f3b709dd0597e6f97a0f74 to your computer and use it in GitHub Desktop.
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