Barbie143
Barbie143

Reputation: 25

Scrape the data and store to dictionary

I was trying to scrape the data from the website link and when we click on the "Sterne Anzeigen" button at the bottom of the review we find the sub ratings and wanted to scrape that rating and store it in the CSV file. I was able to perform the actions like clicking on the button for each review and write code to extract the sub ratings as below

articles=browser.find_elements_by_class_name("index__reviewBlock__27gnB")

data=[]

for article in articles:

try:
   element2 = article.find_elements_by_tag_name('button')
except EX.NoSuchElementException:
   pass

for element in element2:
   ActionChains(browser).click(element).perform()

row={}  

Stars=article.find_elements_by_class_name("index__factor__3Z15R")


for item in Stars:
  key = item.find_element_by_class_name("index__title__W4hOp").text
  value = item.find_element_by_xpath("//*[@class='index__stars__2ads4 index__medium__1wpWb 
   index__stars__3lgvx']").get_attribute("data-score")
  row[key]=value

data.append(row)

Can anyone help me to get the exact ratings since using this code I'm getting all the rating value "5"?

Thanks in advance

Upvotes: 1

Views: 435

Answers (1)

rahul rai
rahul rai

Reputation: 2326

The key here is to only get value of index title and rating belongs to each Review. See below should work:

Edit : It will capture rating and category for all reviews

reviews = driver.find_elements_by_xpath("//div[@class='index__reviewBlock__27gnB']")

data=[]
for rev in reviews:

    row = {}
    rev_Heading = rev.find_element_by_xpath(".//h3").text
    try:
        btn = rev.find_element_by_xpath(".//button[text()='Sterne anzeigen' or text()='Alle anzeigen']") # To click only button with text Sterne anzeigen
        driver.execute_script("arguments[0].scrollIntoView();", btn)
        driver.execute_script("arguments[0].click();", btn)
        time.sleep(1)

        index_titles_xpath = "//div[h3[text()='" + rev_Heading + "']]//following-sibling::div//div[@class='index__block__36tsj index__scoreBlock__1t7Du']//preceding-sibling::h4"
        score_blocks_xpath = "//div[h3[text()='" + rev_Heading + "']]//following-sibling::div//div[@class='index__block__36tsj index__scoreBlock__1t7Du']/span"
        index_titles = driver.find_elements_by_xpath(index_titles_xpath)
        score_blocks = driver.find_elements_by_xpath(score_blocks_xpath)

        for key, value in zip(index_titles, score_blocks):
            row[key.text]  = value.get_attribute("data-score")
        data.append(row)
        btnclose = rev.find_element_by_xpath(".//button[text()='Sterne ausblenden' or text()='Weniger anzeigen']")  #  to click only button with text Sterne ausblenden
        driver.execute_script("arguments[0].scrollIntoView();", btnclose)
        driver.execute_script("arguments[0].click();", btnclose)
        time.sleep(1)
    except NoSuchElementException:
        index_titles_xpath = "//div[h3[text()='"+rev_Heading+"']]//following-sibling::div//div[@class='index__block__36tsj index__scoreBlock__1t7Du']//preceding-sibling::h4"
        score_blocks_xpath = "//div[h3[text()='"+rev_Heading+"']]//following-sibling::div//div[@class='index__block__36tsj index__scoreBlock__1t7Du']//span"
        index_titles = driver.find_elements_by_xpath(index_titles_xpath)
        score_blocks = driver.find_elements_by_xpath(score_blocks_xpath)
        for key, value in zip(index_titles, score_blocks):
            row[key.text] = value.get_attribute("data-score")
        data.append(row)
for d in data:
    print(d)

Out Put: As there are 10 reviews on your page , below output we would get:

enter image description here

Upvotes: 1

Related Questions