Posts

Showing posts from September, 2013

scrape macy's deals using beautiful soup

Let me show you a tiny real example on how to use the bs4 (beautiful soup version 4) module of Python. Say we want to collect information about the hot deals from macy's. The URL is here . Well, you can see all the info in one page and copy-paste, but that's not our purpose. First you have to get the content of the page using the cute requests module. import requests url = 'http://bit.ly/19zWmQT' r = requests.get(url) html_content = r.text Now start cooking the soup: from bs4 import BeautifulSoup soup = BeautifulSoup(html_content) Now look at the html code (page source code) of the url. You will see that the offers are in a list (li) that has 'offer' as a css class name (and some other class names). So you can write the code in the following way: offer_list = soup('li', 'offer') Or you can write: offer_list = soup.find_all('li', 'offer') Another way to write this is: offer_list = soup.select('li.offer')

PHP like trim function in Python

Does python have any trim() function (like php) to remove the leading and trailing whitespace? Answer is yes and it's called strip. Here is the code: >>> str = ' aa b cde ' >>> str ' aa b cde ' >>> str.strip() 'aa b cde' >>> str = str.strip() >>> str 'aa b cde' >>> Happy Coding! :)

calculate age from date of birth

I just needed to write a function in python that calculates age from birthday (date of birth). In stackoverflow I found couple of good discussions here and here . I picked up the following code from the later post, as it's very simple and works for my purpose: def calculate_age(born): today = date.today() try: birthday = born.replace(year=today.year) except ValueError: # raised when birth date is February 29 and the current year is not a leap year birthday = born.replace(year=today.year, day=born.day-1) if birthday > today: return today.year - born.year - 1 else: return today.year - born.year if __name__ == "__main__": day, month, year = [int(x) for x in "7/11/1982".split("/")] born = date(year, month, day) print calculate_age(born)