Search code examples
pythonpython-3.xbeautifulsoupurlliburllib3

Finding a specific element from a website using beautiful soup in Python


I'm trying to write a code that coverts money currencies into each other. To do so, I need the most recent updates of money currencies. I've chosen a website which displays them online. I want to find the desired currency (f.e. USD). I read a few articles about Beautiful Soup, but I'm not able to properly work with it. I've also checked similar questions on Stackoverflow.

Here's the part of HTML code of the currency website I see when I inspect the element:

enter image description here

I've marked down the part which says 270,693. I want to get it with help of soup.find(), but I somehow can't get it right. I'm not really familiar with how it works.

Here's what I've tried, but I know it's not correct:

from bs4 import BeautifulSoup
import urllib.request

url = 'https://english.tgju.org/'
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page.read(), 'html.parser')
rank = soup.find(#What_should I do here?!)
print(rank)

I really appreciate any of your helps in advance.


Solution

  • Try this:

    from bs4 import BeautifulSoup
    import urllib.request
    import pandas as pd
    
    url = 'https://english.tgju.org/'
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page.read(), 'html.parser')
    
    rank = soup.find('tr', {'data-market-row':'sana_sell_usd'})['data-price']
    print(rank)
    

    Output:

    270,639