Python Forum
beatifulsoup scrap td tag. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: beatifulsoup scrap td tag. (/thread-10864.html)



beatifulsoup scrap td tag. - piuk3man - Jun-11-2018

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.


RE: beatifulsoup scrap td tag. - buran - Jun-11-2018

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