Created
September 8, 2015 23:02
-
-
Save vivekseth/65670a25b03f733f58ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import requests | |
from pyquery import PyQuery | |
# Clothing items holds all the info needed to display a clothing item from | |
# any source. | |
class ClothingItem(object): | |
def __init__(self, name, price, imageURL, itemURL): | |
super(ClothingItem, self).__init__() | |
self.name = name | |
self.price = price | |
self.imageURL = imageURL | |
self.itemURL = itemURL | |
# When a list containing ClothingItem objects is printed, this method is | |
# called to create a string representation of the object. | |
def __str__(self): | |
strRepresentation = "\n" # "\n" represents a newline. | |
strRepresentation += "Name: " + self.name + "\n" | |
strRepresentation += "Price: " + self.price + "\n" | |
strRepresentation += "Image: " + self.imageURL + "\n" | |
strRepresentation += "Item: " + self.itemURL + "\n" | |
return strRepresentation; | |
def __repr__(self): | |
return self.__str__() | |
# Returns a list of clothing items from express based on an input string. | |
def listFromSearchString(searchStr): | |
r = requests.get(searchURLFromString(searchStr)) | |
htmlItemList = listFromSearchHTML(r.text); | |
clothingItemList = map(parseSearchItem, htmlItemList); | |
return clothingItemList; | |
# Creates the url for the search string provided as a parameter. | |
def searchURLFromString(search_str): | |
template_url = "http://www.express.com/browse/search.jsp?q=" | |
encoded_str = "+".join(search_str.split(" ")) | |
return template_url + encoded_str | |
# Convert search HTML into a list of html elements that represent the | |
# clothing items. | |
def listFromSearchHTML(htmlStr): | |
d = PyQuery(htmlStr); | |
return d.find("#browse-gallery").find("li.photo"); | |
# Converts an HTML element that represents a clothing item into a | |
# ClothingItem object. Does HTML parsing using PyQuery | |
def parseSearchItem(item): | |
d = PyQuery(item) | |
name = d.find("li.prod-name").text(); | |
price = d.find("li.price").text().split(" ")[0]; | |
imageURL = "http:" + d.find("img").attr("src"); | |
itemURL = "http://www.express.com" + d.find("a").attr("href") | |
return ClothingItem(name, price, imageURL, itemURL) | |
# Start Point. Any lines of code that are not inside a function will run as | |
# soon as the progream starts from top to bottom. Lines of code in functions | |
# are called when the function is called. | |
search_str = sys.argv[1] | |
print listFromSearchString(search_str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment