-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
30 lines (22 loc) · 854 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
from bs4 import BeautifulSoup
import pprint
for page in range(1, 10):
res = requests.get('https://news.ycombinator.com/')
soup = BeautifulSoup(res.text, 'html.parser')
links = soup.select('.titlelink')
subtext = soup.select('.subtext')
def sort_stories_by_vote(hnlist):
return sorted(hnlist, key= lambda k: k['points'], reverse=True)
def create_custom_hn(links, subtext):
hn = []
for inx, item in enumerate(links):
title = item.getText()
href = item.get('href')
vote = subtext[inx].select('.score')
if len(vote):
points = int(vote[0].getText().replace(' points', ''))
if points >= 100:
hn.append({'title': title, 'link': href, 'points': points})
return sort_stories_by_vote(hn)
pprint.pprint(create_custom_hn(links, subtext))