世界货币转换(Python)


# coding: cp936
"""
    世界货币转换
    信息来源网 xe.com
    Author : 寒江风
    QQ : 84640008
    Date : 2006-09-04
"""
import urllib
import re

def main():
    # Show Information
    print '世界货币转换(美元:USD, 人民币:CNY, 欧元:EUR, 英镑:GBP, 日元:JPY)'

    # Build URL Parameters
    dic = {}
    xfrom = raw_input('FROM : ')
    xto = raw_input('TO : ')
    xamount = raw_input('AMOUNT : ')
    dic['From'] = xfrom
    dic['To'] = xto
    dic['Amount'] = xamount

    # Request For The Web Page
    params = urllib.urlencode(dic)
    webpage = urllib.urlopen('http://xe.com/ucc/convert.cgi?%s' % params)
    webdata = webpage.read()

    # Match The Useful Information
    ra = re.search("([,\d]*\.\d*) " + dic['From'], webdata)
    rb = re.search("([,\d]*\.\d*) " + dic['To'], webdata)
    print "转换结果:",
    print ra.group(1) + " " + dic['From'] + " = " + rb.group(1) + " " + dic['To']

if __name__ == '__main__':
    main()

思想:通过网页链接提交进行货币转换查询,获取查询结果,然后进行正则匹配,提取有用信息