用Python里面的Xpath完成一个在线汇率转换器

时间:2022-07-26
本文章向大家介绍用Python里面的Xpath完成一个在线汇率转换器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

阅读本文需要3分钟

首先,它不可以实时的获取汇率的值,每次都需要我们自己去定义一个汇率转换值,这个就会显得不是很智能,有点机械,所以我们这一个利用爬虫爬取一个网址里面的汇率值(一直在更新的),这里我们利用Xpath来获取这个数据值

其次我们发现在之前的程序里面,我们好像只能输入两位数的货币数据,这一次我们通过正负索引的方法,只获取除了最后三个单位的之外的数据即可,灵活的运用,然后输出最后带入单位,最后让输出个更加的美观和直接。

下面我们来看看爬虫数据的代码

首先我们看看这个网址,我们来解析一下这个网页的数据页面

导入库和爬取数据

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))

这里的Xpath语法规则,大家可以移步于初识爬虫之Xpath语法篇看看,其实一条语句就可以解决,非常的方便。

转换程序代码

 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 -*-
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)