As I mentioned in my blog on my first learning resources, right after finishing the Google training, the first script I wrote was a script to get a Dell hardware warranty expiration given the service tag. It was my first attempt at web scraping, something you learn with the Google python videos. I am not sure if this is the best or most efficient way to do this, but it was a way for me to test out what I just learned. If you have some advice, don’t hesitate to share it in the comments.

I’m actually going to do more with this as I learn more. The goal is to access our database of hardware, grab the service tags, get the warranty expiration from Dell, and lastly update a warranty field in the database. From that field, we’ll generate alerts to notify customers of their pending expiration date and hopefully get some warranty sales. This script will be a part of that bigger picture later.

For now, all you do is run dellwarranty.py SERVICETAG to get the date back in a string format.

This script grabs the html and then searches for TopTwoWarrantyListItem. From that starting point, it looks for four greater than brackets to find the beginning position of the date. Then it looks for the very next less than bracket as the ending point for the date.

When I started typing up this blog, I ran the script and it was giving me errors. It worked when I wrote it, but I’ve since rebuilt my laptop with Linux Mint. My laptop was previously running CentOS, which I believe runs Python 2.6 as default. I’m assuming something changed from there. Considering the current training I’m doing is in Python 3, I made the script check for the python version and error out if not version 3.

Without further ado, here’s the Dell warranty script code.

 

 

Author
profilepicJason Vanzin is the CEO at Vanzin Consulting Corp. He has over 15 years of IT experience and lives in Pittsburgh, PA. He blogs on topics related to Business Continuity, Python programming, and technology in general.

1 Comment

  1. Ronald Reynolds says:

    This is great, it gives me all sorts of ideas I never considered before!

    Here is a slightly tweaked version for Python 2.7

    #!/usr/bin/python
    import sys #module used to receive command line arguments into script and check python version
    # if sys.version.startswith(’2.’):
    # print(“Script only runs in Python 3 and above.”)
    # sys.exit(1)
    import urllib2 #module used to request Dell webpage

    def getwarranty(servicetag):

    “”” (str) -> str
    Returns date as string by downloading html as a string and performing a regex search
    to find the date.
    >>>getwarranty(’2M95GR1′)
    1/16/2013
    >>>getwarranty(‘BKTR3G1′)
    4/22/2011
    >>>getwarranty(‘GWQN141′)
    12/18/2004
    “””

    u = “http://www.dell.com/support/troubleshooting/us/en/555/servicetag/” + servicetag + \
    “#ui-tabs-5″
    f = urllib2.urlopen(u)
    contents = str(f.read())
    f.close()
    i = 0
    warranty = “”
    while True:
    i = contents.find(‘TopTwoWarrantyListItem’, i)
    if i == -1:
    break
    i = contents.find(‘>’, i + 1)
    i = contents.find(‘>’, i + 1)
    i = contents.find(‘>’, i + 1)
    i = contents.find(‘>’, i + 1)
    j = contents.find(‘<', i + 1)
    warranty = contents[i+1:j]

    return warranty

    def main():
    args = sys.argv[-1] # Get last word from commandline so it can be checked to see if it's a service tag.
    if not args:
    print('usage: dellwarranty.py [servicetag]')
    sys.exit(1)
    if 'dellwarranty.py' in args:
    print('usage: dellwarranty.py [servicetag]')
    sys.exit(1)
    else:
    servicetag = sys.argv[-1]
    warrantyexpiration = getwarranty(servicetag)
    if warrantyexpiration == "":
    print("Service tag", servicetag, "not found.")
    else:
    print(warrantyexpiration)

    if __name__ == '__main__':
    main()

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

%d bloggers like this: