update:修改了一下输出样式,增加地点的输出

#!/usr/bin/python2

#auth:codewalker #mail:001@codewalker.me
#date: 2013-11-21

from bs4 import BeautifulSoup
import urllib2
import sys

def main():
if len(sys.argv) != 2:
print “Need a track number”
exit(1)
track_number = sys.argv[1]
track_date = []
track_location = []
headers = { ‘User-Agent’ : ‘Mozilla/5.0’ }
url = “https://tools.usps.com/go/TrackConfirmAction!input.action?tRef=qt&tLc=1&tLabels=“ + track_number
req = urllib2.Request(url, None, headers)
htmltext = urllib2.urlopen(req).read()
soup = BeautifulSoup(htmltext)
track_info = soup.find_all(“span”, { “class” : “info-text” })
dates = soup.find_all(“td”, { “class” : “date-time” })
locations = soup.find_all(“td”, { “class” : “location” })

for date in dates:
    d =  ' '.join(date.p.text.split())
    track_date.append(d)

for location in locations:
    l =  ' '.join(location.p.text.split())
    track_location.append(l)

for i in range(len(track_date)):
    print "%s : %s" % (i,track_date\[i\])
    print "%s IN %s" % (track\_info\[i\].text,track\_location\[i\])
    print '#' * 30

if __name__ == “__main__“:
main()
#INPUT:
# ./track.package.py LC945862504US
#OUTPUT:
# 0 : November 21, 2013 , 12:04 am
# Processed at USPS Origin Sort Facility IN BALTIMORE, MD 21233
##############################
# 1 : November 20, 2013 , 6:14 pm
# Dispatched to Sort Facility IN BALTIMORE, MD 21202
##############################
# 2 : November 20, 2013 , 4:07 pm
# Acceptance IN BALTIMORE, MD 21202