statist31
statist31

Reputation: 35

Pulling a Spesific Text of a Part of Website

I'm new on web scraping and BeautifulSoup. I'm making a currency converter via using a site. I use this code to pull currency rate:

import requests
from bs4 import BeautifulSoup

from_ = input("WHICH CURRENCY DO YOU WANT TO CONVERT: ").upper()
to = input("WHICH CURRENCY DO YOU WANT TO CONVERT TO: ").upper()

url = requests.get(f'https://www.xe.com/currencyconverter/convert/?Amount=1&From={from_}&To={to}').text 
soup = BeautifulSoup(url, 'lxml')

currency = soup.find('p', class_ = 'result__BigRate-sc-1bsijpp-1 iGrAod').getText()

print(currency) 

This is okay but it returns a full of text (e.g. 0.84311378 Euros). I want to pull only numbers that marked with red in picture:

Upvotes: 0

Views: 82

Answers (3)

Dan Suciu
Dan Suciu

Reputation: 261

From what I can see, the string you highlighted in the picture represents the first three characters of the resulted price.

This means that any time you try to convert some type of currency to another one, the numbers marked with red will always represent a string with a length equal to 3.

We can pull the information you need by getting a substring of the paragraph’s text. Just replace the last line you provided with:

print(currency[0:4])

This will always return a string containing exactly the characters you are looking for.

Upvotes: 1

Ram
Ram

Reputation: 4779

You can also use .contents and get the first item from it.

currency = soup.find('p', class_ = 'result__BigRate-sc-1bsijpp-1 iGrAod').contents
print(currency[0].strip())
0.84

Upvotes: 1

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

Due to the number would always be the first element of this tag.An easy way could be :

currency_tag = soup.find('p', class_ = 'result__BigRate-sc-1bsijpp-1 iGrAod')

print(next(iter(currency_tag)))

And result:

0.84

Upvotes: 1

Related Questions