Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beatifulsoup scrap td tag.
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Web scrap --Need help Lizardpython 4 954 Oct-01-2023, 11:37 AM
Last Post: Lizardpython
  I tried every way to scrap morningstar financials data without success so far sparkt 2 8,171 Oct-20-2020, 05:43 PM
Last Post: sparkt
  Web scrap multiple pages anilacem_302 3 3,783 Jul-01-2020, 07:50 PM
Last Post: mlieqo
  Need logic on how to scrap 100K URLs goodmind 2 2,570 Jun-29-2020, 09:53 AM
Last Post: goodmind
  Scrap a dynamic span hefaz 0 2,659 Mar-07-2020, 02:56 PM
Last Post: hefaz
  scrap by defining 3 functions zarize 0 1,834 Feb-18-2020, 03:55 PM
Last Post: zarize
  Skipping anti-scrap zarize 0 1,854 Jan-17-2020, 11:51 AM
Last Post: zarize
  Cannot get selenium to scrap past the first two pages newbie_programmer 0 4,134 Dec-12-2019, 06:19 AM
Last Post: newbie_programmer
  Scrap data from not standarized page? zarize 4 3,243 Nov-25-2019, 10:25 AM
Last Post: zarize
  Table data with BeatifulSoup gerry84 11 7,085 Oct-23-2019, 10:09 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020