Skip to content

Instantly share code, notes, and snippets.

@alecxe
Created September 26, 2014 13:27
Show Gist options
  • Save alecxe/a303d06ecb61b5d07b03 to your computer and use it in GitHub Desktop.
Save alecxe/a303d06ecb61b5d07b03 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
""" Test menu for Website
"""
import urllib2
from bs4 import BeautifulSoup
print (47 * '-')
print (" C H O I C E L I S T")
print (47 * '-')
print ("1. Page One")
print ("2. Page Two")
print ("3. Page Three")
print ("4. Page Four")
print (47 * '-')
print (47 * '-')
#############################
## Robust error handling ##
## only accpet int ##
#############################
## Wait for valid input in while...not ###
is_valid=0
while not is_valid :
try :
choice = int ( raw_input('Enter your choice [1-8] : ') )
is_valid = 1 ## set it to 1 to validate input and to terminate the while..not loop
except ValueError, e :
print ("'%s' is not a valid choice." % e.args[0].split(": ")[1])
### Take action as per selected choice list option ###
mapping = {
1: 'www.mywebsite.com/page_one.html',
2: 'www.mywebsite.com/page_two.html',
3: 'www.mywebsite.com/page_three.html',
4: 'www.mywebsite.com/page_four.html'
}
try:
page = mapping[choice]
except KeyError:
print ("Invalid choice. try again...")
# TODO: try again? :)
username = raw_input("Please, type your username\n")
url = "http://{page}/{username}".format(page=page, username=username)
html_content = urllib2.urlopen(url)
soup = BeautifulSoup(html_content, "lxml")
#####################
## STRINGS REPLACE ##
#####################
start_msg = "Hey, you have "
end_msg = "comments !"
end_str = "read !"
####################
## COMMENTS COUNT ##
####################
count_comments = soup.find("span", "sidebar-comments")
count_comments
count_comments_final = count_comments.find_next("meta")
################
## COUNT READ ##
################
count_read = soup.find("span", "sidebar-read")
count_read
count_read_final = count_read.find_next("meta")
##################
## PRINT RESULT ##
##################
print start_msg + count_comments_final['content'].split(':')[1] + end_msg
print start_msg + count_read_final['content'].split(':')[1] + end_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment