Python Forum
How to print just sizes content with splits? - 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: How to print just sizes content with splits? (/thread-28986.html)



How to print just sizes content with splits? - fergar470 - Aug-12-2020

Hi! I´m trying to get just 36.5 | 37..5 ..... sizes and not h2 content. However I´m getting it because I´m doing something wrong. I tested a lot of ways with splits [|], [-1] but not working, not sure why. Hope anyone can help me thanks.
Here you have picture of content I want to get and code I have.

At the moment result I´m getting is
"Sizes available" + sizes. But is just want sizes.

my code:
sizes = url.find("div", "available-sizes")
sizes = sizes.text.split("\\n")[-1]
[Image: mz0NfdF]


RE: How to print just sizes content with splits? - snippsat - Aug-13-2020

Post code in code tag as mention,and do not have code shown in a image,then have to recreate it to test it out.
from bs4 import BeautifulSoup

html = '''\
<!DOCTYPE html>
<html>
<body>
  <div class="available-sizes">
    <h2>Sizes Available</h2>
    " 37 | 38 | 39 | 42,5 "
  </div>
</body>
</html>'''

soup = BeautifulSoup(html, 'lxml')
Test usage.
>>> tag_size = soup.find('div', class_="available-sizes")
>>> tag_size
[python]<div class="available-sizes">
<h2>Sizes Available</h2>
    " 37 | 38 | 39 | 42,5 "
  </div>

>>> tag_size.text
'\nSizes Available\n    " 37 | 38 | 39 | 42,5 "\n  '

>>> tag_size.text.split('"')[1].strip()
'37 | 38 | 39 | 42,5'