用Python制作汇率转换小程序

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

PS:如有需要Python学习资料的小伙伴可以点击下方链接自行获取

Python免费学习资料、代码以及交流解答点击即可加入


1.分析网页

下面我们来看看爬虫数据的代码,首先我们看看这个网址:

https://www.huilv.cc/USD_CNY/

我们来分析一下这个网页的数据页面:

 

 

2.爬取数据

import requests
from lxml import etree

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
}
url = "https://www.huilv.cc/USD_CNY/"


def Get_huilv(url, headers1):
    res = requests.get(url=url, headers=headers1, timeout=2)
    # print(res.status_code)#打印状态码
    html = etree.HTML(res.text)
    USD_VS_RMB_0 = html.xpath('//div[@id="main"]/div[1]/div[2]/span[1]/text()')
    for a in USD_VS_RMB_0:
        b = a
    USD_VS_RMB_1 = float(b)
    print("实时汇率为:{}".format(USD_VS_RMB_1))

转换程序代码:

 currency_str_value = 0
    while currency_str_value != "":
        USD_VS_RMB = float(str(USD_VS_RMB_1))
        # 输入带单位的货币金额
        currency_str_value = input('请输入带单位货币的金额: ')
        # 获取货币单位
        unit = currency_str_value[-3:].upper()  # 第一次判断
        if unit == 'CNY':
            exchange_rate = 1 / USD_VS_RMB
            string = "美元"
        elif unit == 'USD':
            exchange_rate = USD_VS_RMB
            string = "元"
        else:
            exchange_rate = -1
        if exchange_rate != -1:
            in_money = eval(currency_str_value[0:-3])
            # 使用lambda定义函数
            convert_currency2 = lambda x: x * exchange_rate
            # 调用lambda函数
            out_money = convert_currency2(in_money)
            print('转换后的金额是:{} {} '.format(round(out_money), string))
        else:
            print('无法计算')

其实里面没有什么难点,只是对于一些语法不够熟练的小伙伴来说有一点难,不过多看几次就好了。

全部代码:

# -*- coding :  utf-8 -*-
# @Software  :  PyCharm
# @File      :  汇率实时计算.py
# @CSDN      :  https://blog.csdn.net/weixin_47723732
import requests
from lxml import etree

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
}
url = "https://www.huilv.cc/USD_CNY/"


def Get_huilv(url, headers1):
    res = requests.get(url=url, headers=headers1, timeout=2)
    # print(res.status_code)#打印状态码
    html = etree.HTML(res.text)
    USD_VS_RMB_0 = html.xpath('//div[@id="main"]/div[1]/div[2]/span[1]/text()')
    for a in USD_VS_RMB_0:
        b = a
    USD_VS_RMB_1 = float(b)
    print("实时汇率为:{}".format(USD_VS_RMB_1))

    currency_str_value = 0
    while currency_str_value != "":
        USD_VS_RMB = float(str(USD_VS_RMB_1))
        # 输入带单位的货币金额
        currency_str_value = input('请输入带单位货币的金额: ')
        # 获取货币单位
        unit = currency_str_value[-3:].upper()  # 第一次判断
        if unit == 'CNY':
            exchange_rate = 1 / USD_VS_RMB
            string = "美元"
        elif unit == 'USD':
            exchange_rate = USD_VS_RMB
            string = "元"
        else:
            exchange_rate = -1
        if exchange_rate != -1:
            in_money = eval(currency_str_value[0:-3])
            # 使用lambda定义函数
            convert_currency2 = lambda x: x * exchange_rate
            # 调用lambda函数
            out_money = convert_currency2(in_money)
            print('转换后的金额是:{} {} '.format(out_money, string))
        else:
            print('无法计算')

Get_huilv(url, headers)

3.效果演示

下面我们来看看演示效果:

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值