利用Python中的Xpath實(shí)現(xiàn)一個(gè)在線匯率轉(zhuǎn)換器
前言
在之前的語(yǔ)法里面,我們記得有一個(gè)初識(shí)Python之匯率轉(zhuǎn)換篇,在那個(gè)程序里面我們發(fā)現(xiàn)可以運(yùn)用一些基礎(chǔ)的語(yǔ)法寫一個(gè)匯率計(jì)算,但是學(xué)到后面的小伙伴就會(huì)發(fā)現(xiàn)這個(gè)小程序有一定的弊端。
首先,它不可以實(shí)時(shí)的獲取匯率的值,每次都需要我們自己去定義一個(gè)匯率轉(zhuǎn)換值,這個(gè)就會(huì)顯得不是很智能,有點(diǎn)機(jī)械,所以我們這一個(gè)利用爬蟲爬取一個(gè)網(wǎng)址里面的匯率值(一直在更新的),這里我們利用Xpath來(lái)獲取這個(gè)數(shù)據(jù)值
其次我們發(fā)現(xiàn)在之前的程序里面,我們好像只能輸入兩位數(shù)的貨幣數(shù)據(jù),這一次我們通過(guò)正負(fù)索引的方法,只獲取除了最后三個(gè)單位的之外的數(shù)據(jù)即可,靈活的運(yùn)用,然后輸出最后帶入單位,最后讓輸出個(gè)更加的美觀和直接。
下面我們來(lái)看看爬蟲數(shù)據(jù)的代碼
首先我們看看這個(gè)網(wǎng)址,我們來(lái)解析一下這個(gè)網(wǎng)頁(yè)的數(shù)據(jù)頁(yè)面
導(dǎo)入庫(kù)和爬取數(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('實(shí)時(shí)匯率為:{}'.format(USD_VS_RMB_1))
這里的Xpath語(yǔ)法規(guī)則,大家可以移步于初識(shí)爬蟲之Xpath語(yǔ)法篇看看,其實(shí)一條語(yǔ)句就可以解決,非常的方便。
轉(zhuǎn)換程序代碼
currency_str_value = 0 while currency_str_value != '': USD_VS_RMB = float(str(USD_VS_RMB_1)) # 輸入帶單位的貨幣金額 currency_str_value = input(’請(qǐng)輸入帶單位貨幣的金額: ’) # 獲取貨幣單位 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(’無(wú)法計(jì)算’)
其實(shí)里面沒有什么難點(diǎn),只是對(duì)于一些語(yǔ)法不夠熟練的小伙伴來(lái)說(shuō)有一點(diǎn)難,不過(guò)多看幾次就好了
下面我們來(lái)看看演示效果
全部代碼
# -*- coding : utf-8 -*-# @Time : 2020/9/8 12:37# @author : 王小王# @Software : PyCharm# @File : 匯率實(shí)時(shí)計(jì)算.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('實(shí)時(shí)匯率為:{}'.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(’請(qǐng)輸入帶單位貨幣的金額: ’) # 獲取貨幣單位 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(’無(wú)法計(jì)算’)Get_huilv(url, headers)
總結(jié)
到此這篇關(guān)于利用Python中的Xpath實(shí)現(xiàn)一個(gè)在線匯率轉(zhuǎn)換器的文章就介紹到這了,更多相關(guān)Python Xpath實(shí)現(xiàn)在線匯率轉(zhuǎn)換器內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解2. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解3. Yii2.0引入CSS,JS文件方法4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析5. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. vue使用webSocket更新實(shí)時(shí)天氣的方法7. 淺談python出錯(cuò)時(shí)traceback的解讀8. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解9. Nginx+php配置文件及原理解析10. JavaMail 1.4 發(fā)布
