Python Forum

Full Version: beatifulsoup scrap td tag.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,I 'm learning python and beatiful soup. Unfortunately, theory 's one thing and practise is another. I 'm trying to scrape a site, and to be more accurate a tag / style. Here 's the code.

# Import libraries
import requests
from bs4 import BeautifulSoup

URL = "https://www.preciodolar.com.ar"
r = requests.get(URL)
# print(r.content)

# Create a BeautifulSoup object
soup = BeautifulSoup(r.content)
print(soup.prettify())

dolar = soup.find_all("td", class_="Estilo4")
print (dolar)
output:

[<td class="Estilo4">24.98</td>, <td class="Estilo4">25.98</td>]

Till here it 's OK, but what I relly need it's the numbers between the TD tags, both of them(first and second TD).

Is there a way to extract them in order to save it in a variable.
what you get is list of tag elements, so you need to use .text property to get the text you want

# Import libraries
import requests
from bs4 import BeautifulSoup
 
URL = "https://www.preciodolar.com.ar"
r = requests.get(URL)
# print(r.content)
 
# Create a BeautifulSoup object
soup = BeautifulSoup(r.text, 'html.parser')
 
td_tags = soup.find_all("td", class_="Estilo4")
print ([td.text for td in td_tags])
Output:
[u'24.98', u'25.98']
I also added explicit html.parser to avoid getting warning