利用Python中的Xpath實現(xiàn)一個在線匯率轉(zhuǎn)換器
前言
在之前的語法里面,我們記得有一個初識Python之匯率轉(zhuǎn)換篇,在那個程序里面我們發(fā)現(xiàn)可以運用一些基礎(chǔ)的語法寫一個匯率計算,但是學(xué)到后面的小伙伴就會發(fā)現(xiàn)這個小程序有一定的弊端。
首先,它不可以實時的獲取匯率的值,每次都需要我們自己去定義一個匯率轉(zhuǎn)換值,這個就會顯得不是很智能,有點機械,所以我們這一個利用爬蟲爬取一個網(wǎng)址里面的匯率值(一直在更新的),這里我們利用Xpath來獲取這個數(shù)據(jù)值
其次我們發(fā)現(xiàn)在之前的程序里面,我們好像只能輸入兩位數(shù)的貨幣數(shù)據(jù),這一次我們通過正負索引的方法,只獲取除了最后三個單位的之外的數(shù)據(jù)即可,靈活的運用,然后輸出最后帶入單位,最后讓輸出個更加的美觀和直接。
下面我們來看看爬蟲數(shù)據(jù)的代碼
首先我們看看這個網(wǎng)址,我們來解析一下這個網(wǎng)頁的數(shù)據(jù)頁面
導(dǎo)入庫和爬取數(shù)據(jù)
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)#打印狀態(tài)碼 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語法規(guī)則,大家可以移步于初識爬蟲之Xpath語法篇看看,其實一條語句就可以解決,非常的方便。
轉(zhuǎn)換程序代碼
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定義函數(shù) convert_currency2 = lambda x: x * exchange_rate # 調(diào)用lambda函數(shù) out_money = convert_currency2(in_money) print(’轉(zhuǎn)換后的金額是:{} {} ’.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)#打印狀態(tài)碼 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定義函數(shù) convert_currency2 = lambda x: x * exchange_rate # 調(diào)用lambda函數(shù) out_money = convert_currency2(in_money) print(’轉(zhuǎn)換后的金額是:{} {} ’.format(out_money, string)) else: print(’無法計算’)Get_huilv(url, headers)
總結(jié)
到此這篇關(guān)于利用Python中的Xpath實現(xiàn)一個在線匯率轉(zhuǎn)換器的文章就介紹到這了,更多相關(guān)Python Xpath實現(xiàn)在線匯率轉(zhuǎn)換器內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python調(diào)用接口合并Excel表代碼實例2. .net如何優(yōu)雅的使用EFCore實例詳解3. Python快速將ppt制作成配音視頻課件的操作方法4. ASP.Net Core對USB攝像頭進行截圖5. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計要點6. 通過CSS數(shù)學(xué)函數(shù)實現(xiàn)動畫特效7. ASP.NET MVC使用Boostrap實現(xiàn)產(chǎn)品展示、查詢、排序、分頁8. 通過Ajax方式綁定select選項數(shù)據(jù)的實例9. ASP.NET MVC實現(xiàn)橫向展示購物車10. ajax動態(tài)加載json數(shù)據(jù)并詳細解析
