-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.py
More file actions
31 lines (23 loc) · 941 Bytes
/
scrape.py
File metadata and controls
31 lines (23 loc) · 941 Bytes
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
30
31
import requests
from bs4 import BeautifulSoup
import pprint
def sort_stories_by_votes(list):
return sorted(list, key=lambda k: k['votes'], reverse = True);
def create_custom_hn():
hn = []
for i in range(1, 2):
response = requests.get('https://news.ycombinator.com/news?p=' + str(i))
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.select('.storylink')
votes = soup.select('.score')
subtext = soup.select('.subtext')
for idx, item in enumerate(links):
title = item.getText()
href = item.get('href', '')
vote = subtext[idx].select('.score')
if len(vote):
points = int(vote[0].getText().replace(' points', ''))
if points > 99:
hn.append({'title': title, 'link': href, 'votes': points})
return sort_stories_by_votes(hn)
pprint.pprint(create_custom_hn())