利用Python中的Xpath實現一個在線匯率轉換器
前言
在之前的語法里面,我們記得有一個初識Python之匯率轉換篇,在那個程序里面我們發現可以運用一些基礎的語法寫一個匯率計算,但是學到后面的小伙伴就會發現這個小程序有一定的弊端。
首先,它不可以實時的獲取匯率的值,每次都需要我們自己去定義一個匯率轉換值,這個就會顯得不是很智能,有點機械,所以我們這一個利用爬蟲爬取一個網址里面的匯率值(一直在更新的),這里我們利用Xpath來獲取這個數據值
其次我們發現在之前的程序里面,我們好像只能輸入兩位數的貨幣數據,這一次我們通過正負索引的方法,只獲取除了最后三個單位的之外的數據即可,靈活的運用,然后輸出最后帶入單位,最后讓輸出個更加的美觀和直接。
下面我們來看看爬蟲數據的代碼
首先我們看看這個網址,我們來解析一下這個網頁的數據頁面
導入庫和爬取數據
import requestsfrom lxml import etreeheaders = { '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 -*-# @Time : 2020/9/8 12:37# @author : 王小王# @Software : PyCharm# @File : 匯率實時計算.py# @CSDN : https://blog.csdn.net/weixin_47723732import requestsfrom lxml import etreeheaders = { '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)
總結
到此這篇關于利用Python中的Xpath實現一個在線匯率轉換器的文章就介紹到這了,更多相關Python Xpath實現在線匯率轉換器內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
