Python Forum

Full Version: Web scraping beautifulsoup easy question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have a code like belowe

from bs4 import BeautifulSoup
import requests

r = requests.get("https://www.walutomat.pl/kursy-walut/")
soup = BeautifulSoup(r.content, "html.parser")
EUR = soup.find_all("tr", {"class": "EUR_PLN"}, "span", {"class": "sell"})

USD = soup.find_all("tr", {"class": "USD_PLN"})
CHF = soup.find_all("tr", {"class": "CHF_PLN"})
GBP = soup.find_all("tr", {"class": "GBP_PLN"})
for cena_EUR in EUR:
    print(cena_EUR.text)

for cena_USD in USD:
    print(cena_USD.text)

for cena_CHF in CHF:
    print(cena_CHF.text)

for cena_GBP in GBP:
    print(cena_GBP.text)
The line
EUR = soup.find_all("tr", {"class": "EUR_PLN"}, "span", {"class": "sell"})
doesnt working like i want. I have html code like this
<tr class="EUR_PLN"><td>
<a href="eur-pln/" class="pair">EUR / PLN</a></td>
<td class="exchange-rate"><span class="rate exchange-rate-value"><span class="sell">4,3068</span> <span class="currency">PLN</span></span></td><td class="exchange-rate"><span class="rate exchange-rate-value"><span class="buy">4,3050</span> <span class="currency">PLN</span></span></td><td class="exchange-rate"><span class="rate exchange-rate-value"><span class="forex">4,3044</span> <span class="currency">PLN</span></span></td><td class="more-link"><a href="eur-pln/" class="icon-arrow-right">więcej</a></td></tr>


and i want to get 4,3068. How to do it?

USD = soup.find_all("tr", {"class": "USD_PLN"})
CHF = soup.find_all("tr", {"class": "CHF_PLN"})
GBP = soup.find_all("tr", {"class": "GBP_PLN"})
works great, but i dont need so many information.

Thanks for help and sorry for my English, im 14.
First, how to make what you want
from bs4 import BeautifulSoup
import requests

currencies = ['EUR', 'USD', 'CHF', 'GBP']
r = requests.get('https://www.walutomat.pl/kursy-walut/')
soup = BeautifulSoup(r.text, "html.parser")

for currency in currencies:
    fx_rate = soup.find("tr", {"class": "{}_PLN".format(currency)}).find("span", {"class": "sell"})
    print('1 {} = {} PLN'.format(currency, fx_rate.text))
However note that the exchange rate is not correct - i.e. it looks like some fixed placeholder value that is updated after page load/refresh. Then, if you investigate further what requests are made within the browser you will notice that information comes as json. And is way more convenient to access it. Also BeautifulSoup is not required. You can use either single request to get https://user.walutomat.pl/api/public/marketBrief/ or multiple requests - one per currency pair to (example) https://user.walutomat.pl/api/public/mar...ef/EUR_PLN

import requests
r = requests.get('https://user.walutomat.pl/api/public/marketBrief/')
data = r.json()

for item in data:
    if item['pair'].endswith('PLN'):
        currency =  item['pair'].split('_')[0]
        fx_rate = item['bestOffers']['ask_now']
        print('1 {} = {} PLN'.format(currency, fx_rate))
note the result is not fixed ordered.
using json with multiple requests (result is always ordered)
import requests

currencies = ['EUR', 'USD', 'CHF', 'GBP']
for currency in currencies:
    r = requests.get('https://user.walutomat.pl/api/public/marketBrief/{}_PLN'.format(currency))
    data = r.json()
    fx_rate = data['bestOffers']['ask_now']
    print('1 {} = {} PLN'.format(currency, fx_rate))
You are a genius. IQ above 160. Now im going to make a some UI in tkinter.
(Jul-18-2018, 03:16 PM)Dzonzi Wrote: [ -> ]Where are u get https://user.walutomat.pl/api/public/marketBrief/ from?

from https://www.walutomat.pl/kursy-walut/

the other one - https://user.walutomat.pl/api/public/mar...ef/EUR_PLN
is from
https://www.walutomat.pl/kursy-walut/eur-pln/

use developer tools in your browser to check network trafic and what requests were sent when load page.