我正在练习抓取网站,我收到了一连串的价格。我不太熟悉列表以及它们如何工作所以我不确定,但我想将美元转换为澳元,这大约只是1美元:1.32美元的比率。我假设字符串是第一个eval()成为浮点数列表,然后可能只乘以1.32,但我不确定如何实际进行比率交换:
from tkinter import *
from re import findall, MULTILINE
rss = open('rss.xhtml', encoding="utf8").read()
# prints 10 price values
regex_test = findall(r'([0-9]+[.]*[0-9]*) USD', rss)
price = ["$" + regex_test for regex_test in regex_test]
for cost in range(10):
print(price[cost])
那将打印10个价格,其中=>代表向下一个价格的过渡,即20美元变为26.40澳元:
为了帮助使用相同的正则表达式来提高价格,这是类似的RSS Feed https://www.etsy.com/au/shop/ElvenTechnology/rss
使用了10个范围,因为我不想刮掉数百个条目,只有少数几个条目。
答案 0 :(得分:1)
让你的for循环更加pythonic:
from tkinter import *k from re import findall, MULTILINE
rss = open('rss.xhtml', encoding="utf8").read()
# prints 10 price values
regex_test = findall(r'([0-9]+[.]*[0-9]*) USD', rss)
price = ["$" + regex_test for regex_test in regex_test]
for individual_price in price:
print(individual_price)
将列表转换为AUD,假设您只想乘以一个值,对于您的代码,最好在添加美元符号之前返回列表:
aud_usd_ratio = 1.32 # 1.32 AUD to 1 USD
aud_price_list = ["$" + str(float(x)*aud_usd_ratio) for x in regex_test]
print(aud_price_list)
如果你需要这两个小数位,你也可以使用字符串格式:
aud_price_list = ["${:.2f}".format(float(x)*aud_usd_ratio ) for x in regex_test]
print(aud_price_list)
答案 1 :(得分:1)
稍微改变一下glycoaddict的解决方案,更新价格或同样的变量"变量"可以在列表中创建,然后单独调用列表中的每个值:
# installs necessary modules
from tkinter import *
from re import findall, MULTILINE
import urllib.request
# downloads an rss feed to use, the feel is downloaded,
# then saved under name and format (xhtml, html, etc.)
urllib.request.urlretrieve("https://www.etsy.com/au/shop/ElvenTechnology/rss", "rss.xhtml")
# opens the downloaded file to read from, 'U' can be used instead
# of 'encoding="utf8"', however this causes issues on some feeds, for
# example this particulare feed needs to be encoded in utf8 otherwise
# a decoding error occurs as shown below;
# return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError:
# 'charmap' codec can't decode byte 0x9d in position 12605: character maps to <unidentified>
rss = open('rss.xhtml', encoding="utf8").read()
# regex is used to find all instances within the document which was opened
# and called rss
regex_test = findall(r'([0-9]+[.]*[0-9]*) USD', rss)
# formats the returned string to be modified to desired value (glycoaddict)
# aud_usd_ratio = 1.32 is the same as simply using 1.32, this just creates
# a variable with a value of 1.32 to multuply rather than simply 1.32 itself
AUD_price = ["${:.2f}".format(float(USD)*1.32) for USD in regex_test]
# loops the function 10 times, this is to stop rss feeds with thousands
# of returns listing endlessly, this only returns the first 10, which are
# taken out of the created and formatted/modified string list, and prints
# each value individually, which is useful for say a list of label
# in tkinter to be looped and placed
for individual_item_price in range(10):
print(AUD_price[individual_item_price])
请注意,每次运行时都会下载并更新rss文件,这意味着可以将其视为实时价格,现在运行,然后一小时或几个小时将返回不同的结果。
答案 2 :(得分:0)
假设regex_test
与我的prices_list_usd
相同:
prices_list_usd = [11.11,12.22,21.324,3.11]
usd_aud_ratio = 1.32
prices_list_aud = [price*usd_aud_ratio for price in prices_list_usd]
combined_list = zip(prices_list_usd,prices_list_aud)
for pair in combined_list:
print("$USD {0} => $AUD {1}".format(pair[0],pair[1]))
答案 3 :(得分:0)
我认为您需要提取所有值,将它们转换为float,然后相应地格式化,
# I don't know rss file so dummy variable
rss = "$20.00 => $26.40 $20.00 => $26.40 $16.00 => $21.12 $189.00 => $249.48"
costs = re.findall(r'(?<=\$)\d+\.\d+', rss)
# cast to float and multiply with 1.32
costs = [float(cost) * 1.32 for cost in costs]
# now format them
for i in range(0, len(costs), 2):
print("{:.2f} => {:.2f}".format(costs[i], costs[i + 1]))
# output
# 26.40 => 34.85
# 26.40 => 34.85
# 21.12 => 27.88
# 249.48 => 329.31