How to obtain currency rates from this website con

2019-09-19 03:59发布

问题:

How can I implement the currency rates on this website and keep the currencies up to date so that i can access them in python from this website and input and output values and currencies types. I need my program to connect to this webpage and take the rates of the currency using the currency converter app on the webpage.

This is the website

I need to know how to do this on many high street currency providers that have a website however many do not supply a list of rates so web scrapping is not as straightforward.

Which modules will i need to install using pip and how will i be able to use them to achieve my goal. Many thanks

回答1:

You can use the following code where exchange_rates is a dict mapping currencies to their rates (the url is the one that this page uses to load it's data)

import requests
import json

url = "https://api.travelex.net/salt/config/multi?key=Travelex&site=%2Fvirgin&options=abhikzl"
r = requests.get(url)
data = json.loads(r.text)
exchange_rates = data['rates']['rates']

for item in exchange_rates:
    print("{}, {}".format(item, exchange_rates[item]))

You can apply this to most sites by loading them with the chrome developer network window open and look through the requests to find the one that loads in this data.