DigiKey Price Parser

Needed to hit DigiKey to look up a load of prices, sooo...

#!/usr/bin/env python
import urllib2
from BeautifulSoup import BeautifulSoup
import sys

id = sys.argv[1]
if len(sys.argv) > 2:
    need = int(sys.argv[2])
else:
    need = 1
    
page = urllib2.urlopen( \
"http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail?name=" + id)

soup = BeautifulSoup(page)

pricetable = soup.find('table', frame='void')
prices = pricetable.findChildren('tr')[1:]
for price in prices:
    quantity = price.first('td')
    if int(quantity.contents[0]) > need:
        cost = quantity.nextSibling
        print cost.contents[0]
        break

Posted on 21 Feb 2008
With thanks to https://github.com/jamesyu/jamesyu_jekyll_template