solidsnake solidsnake - 4 months ago 8
Python Question

How do i extract values with beautifulsoup?

Hi am using beautiful soup to extract the euro to us value, this is what i got so far:

import requests
from bs4 import BeautifulSoup

def Euro_spider():
url = 'http://fx-rate.net/USD/'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")


what should i do next?

Answer

Now you need to locate the correct element containing the rate:

<a href="/USD/EUR/" class="1rate" title="Dollar to Euro">0.895</a>

You can locate it, for example, by title:

usd_to_euro = soup.find(title="Dollar to Euro").get_text()
print(usd_to_euro)  # prints 0.895
Comments