I'm trying to get the result out so that I'll be able to get what's inside of "width: 100%" and only the number part. I'm using beautifulsoup python. and my code is here for getting it:
rating_data.append(e.select_one('.review-list__rating__active'))
but it keeps giving me the whole entire line which is
<span style="width: 100%">
above result. I'm trying to get the value inside of the width. is that any possible using beautifulsoup, python? Thanks guys.
CodePudding user response:
To get the values of an attribute use get()
:
e.select_one('.review-list__rating__active').get('style')
If there is only one style you could use split()
to get the value:
e.select_one('.review-list__rating__active').get('style').split(':')[-1]
If there are multiple you have to iterate:
for s in e.select_one('.review-list__rating__active').get('style').split(';'):
if 'with' in s:
print(s.split(':')[-1])
Another alternativ would be to use regex
.
CodePudding user response:
The cssutils
library can help with this:
from bs4 import BeautifulSoup
import cssutils
html = """<span style="width: 100%"></span>"""
soup = BeautifulSoup(html, "html.parser")
span_style = soup.select_one('.review-list__rating__active')['style']
style = cssutils.parseStyle(span_style)
print(style.width)
This displays:
100%