-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathhackernews.py
More file actions
executable file
·33 lines (26 loc) · 986 Bytes
/
hackernews.py
File metadata and controls
executable file
·33 lines (26 loc) · 986 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
32
33
#!/usr/bin/env python
from __future__ import print_function
import argparse
import requests
from bs4 import BeautifulSoup
# check to see if a number was passed in
parser = argparse.ArgumentParser()
parser.add_argument(
'news_count', metavar='int', type=int, choices=range(1,31),
nargs='?', default=10,
help='Then number of news items to return, from 1 to 30')
args = parser.parse_args()
# get the front page from hacker news
response = requests.request("GET", "https://news.ycombinator.com/")
# convert the response to soup
soup = BeautifulSoup(response.text, "lxml")
# process all of the things! :D
for things in soup("tr", { "class" : "athing" })[:args.news_count]:
# get at the rank of each thing
for rank in things("span", { "class" : "rank" }):
print( rank.text, end=' ' )
# get the title of each thing
for title in things("a", { "class" : "storylink" }):
print( title.text )
print( title['href'] )
print( " " )