solidsnake solidsnake - 1 year ago 54
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 = ''
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")

what should i do next?

Answer Source

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