From 56e6303b45105e8b99772775b36f4c5c736345dd Mon Sep 17 00:00:00 2001 From: Michael Jenkins Date: Wed, 25 Oct 2017 21:53:04 -0700 Subject: [PATCH 1/2] --- Awesome-Scripts/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Awesome-Scripts/.DS_Store diff --git a/Awesome-Scripts/.DS_Store b/Awesome-Scripts/.DS_Store deleted file mode 100644 index e92482c21dcc960b5b35a2adfc77a0eda35656e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%T59@6g@>2_*h^x(UqB{8%bOnlL^6vi7Wj8gyA7^cnG4fn~i^BOkDXXevBWW z=iW9EhG8qj^pf6FIz7F;XJ*IOZV5*+Gjv>zr{_vG zb?KnfBLFeaW;NF3Ge9_j!_Z-3kymKOQi+yo+!4cAI@<%+FLan#v~(DE_%JTAaVHd` z(K&vg-C;tD-g^bS0`m%N*uO>D|1XxG|K~~mpI5*u@UIjw`N~bD%#_^Tx{#dgwE@d5 qi Date: Wed, 25 Oct 2017 22:06:47 -0700 Subject: [PATCH 2/2] add argparse, return number of items requested --- Awesome-Scripts/hackernews.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Awesome-Scripts/hackernews.py b/Awesome-Scripts/hackernews.py index 5f1c8d9..7efa906 100755 --- a/Awesome-Scripts/hackernews.py +++ b/Awesome-Scripts/hackernews.py @@ -1,20 +1,27 @@ #!/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") -# count the things that get processed -count = 0 - # process all of the things! :D -for things in soup("tr", { "class" : "athing" }): +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=' ' ) @@ -24,7 +31,3 @@ print( title.text ) print( title['href'] ) print( " " ) - - count = count + 1 - - if count == 10: break