Python Forum
How to print just sizes content with splits?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print just sizes content with splits?
#1
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]
Reply
#2
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting a string by different splits Skaperen 9 6,486 Feb-09-2017, 08:59 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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