用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.效果演示

下面我們來看看演示效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM