Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNat Rogers
7,433 PointsHelp with python's beautiful soup / HTML question
Hi all, I'm trying pull the value '52' out of this HTML code, but I can't seem to get the bs4 selector right. Due to the format of the website I need to specify the div class name 'hi-low' and then further specify a span class name 'hi'. This is what I've come up with so far. The first print() call prints out a string with the correct info in it. I just can't get the '52' value
My code:
import bs4
import requests
res = requests.get('https://wunderground.com/weather/us/md/baltimore')
res.raise_for_status()
w_u_soup = bs4.BeautifulSoup(res.text, "html.parser")
hi_lo_div = w_u_soup.find('div', class_='hi-lo')
print(hi_lo_div)
for element in hi_lo_div.select('span .hi'):
print(element.getText())
This is the only output I get <div _ngcontent-c21="" class="hi-lo"><span _ngcontent-c21="" class="hi">52°</span> <span _ngcontent-c21="" class="separator">|</span> <span _ngcontent-c21="" class="lo">30°</span></div>
1 Answer
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsMaybe the docs would be helpful
# docs example
soup.find_all("a", class_="sister")
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
# try this
soup.find_all("span", class_="hi")