Issue
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:
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)
Solution
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%']
Answered By - baduker
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.