How to extract style value from html span value?

Viewed 117

i write i code in python using beautifulsoup to extract data from online shopping site but i have an issue in extract ratting as it was written in html as shown below:

Sample HTML

my procedure was as following and i need a better way :

result = requests.get("https://egypt.souq.com/eg-en/laptops-rgx2060/s/?as=1")
src = result.content  # .content
soup = BeautifulSoup(src, 'lxml')

rating = []
rate = soup.find_all("span", {"class": "rating-stars"})
for n in range(len(rate)):
    rate_txt = ""

    zz = re.findall(r"\d", str(rate[n].find("i", {"class": "star-rating-svg"}).find("i")))
    for j in range(len(zz)):
        rate_txt += zz[j]

    rate_txt += "%"
    rating.append(rate_txt)
2 Answers

You might want to try this:

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(
    requests.get("https://egypt.souq.com/eg-en/laptops-rgx2060/s/?as=1").content,
    'lxml',
)

rating = []
for i in soup.find_all("i", {"class": "star-rating-svg"}):
    try:
        rating.append(i.find("i")["style"].rsplit(":")[-1])
    except KeyError:
        continue

print(rating)

Output:

['0%', '94%', '80%', '88%', '74%', '80%', '90%', '80%', '74%', '80%', '80%', '82%', '82%', '76%', '68%', '90%', '80%', '20%', '94%', '100%', '96%', '20%', '0%', '0%']

using ("i", {"class": "star-rating-svg"}) will select rating value on the left sidebar this is why you need try catch, instead find rating that in product container or .single-item .star-rating-svg i

rating = []
rate = soup.select(".single-item .star-rating-svg i") # or: .single-item i i
for r in rate:
    rating.append(r["style"].replace('width:', ''))

print(rating)
Related