简体   繁体   中英

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 (fe 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:

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM