從Google Play商店網站中提取特定應用的所有評論


0
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


class FindByXpathCss():

    Reviews = []
    reviewText = []
    driver = webdriver.Chrome(executable_path=r"F:\Chrome-webdriver\chromedriver.exe")
    driver.maximize_window()
    baseUrl = "https://play.google.com/store/apps/details? 
    id=com.delta.mobile.android&hl=en_US&showAllReviews=true"
    driver.get(baseUrl)

    driver.find_element_by_tag_name('body').send_keys(Keys.ARROW_DOWN)
    time.sleep(3)

    review_btn = driver.find_elements_by_xpath("//button[contains(@class,'')][contains(text(),'Full 
    Review')]")

    for btn in review_btn:
        btn.click()

即使我使用列表類型來獲取所有評論,我也只能獲得一個評論,請幫助打印所有評論。而且,它並沒有獲得所有評論(僅獲得一些評論)

    for textreview in reviewText:
        Reviews.append(textreview.text)
        print(Reviews)

        driver.close()
1

Below is the code for doing that in java , you can use the same logic in python.

The logic is:

  1. Get all displayed review, store the length to a variable 'tmp'
  2. Scroll to last review
  3. Check whether show more is displayed, if displayed then click it
  4. Get all reviews again, check whether the current length == 'tmp' , set tmp to new value
  5. Continue scrolling to last element and clicking show more, till current length== 'tmp'

       driver.manage().window().maximize();
           String baseUrl = "https://play.google.com/store/apps/details?id=com.delta.mobile.android&hl=en_US&showAllReviews=true";
           driver.get(baseUrl);
           List<WebElement> a;
    
           //find all the reviews
           a= driver.findElements(By.cssSelector("span[jsname=\"bN97Pc\"]"));
           int b = 0;
           JavascriptExecutor js = (JavascriptExecutor) driver;
           WebElement showmore;
    
           //scroll to last element
           while(a.size()!=b) {
    
               b=a.size();
               System.out.println("size of b is "+ b); 
               js.executeScript("arguments[0].scrollIntoView();",  a.get(a.size() - 1));
               Thread.sleep(3000);
    
               Boolean isPresent = driver.findElements(By.xpath("//*[contains(text(),\"Show More\")]/../..")).size() > 0;
    
               if(isPresent) {
                   showmore= driver.findElement(By.xpath("//*[contains(text(),\"Show More\")]/../.."));
                   showmore.click();
                   Thread.sleep(2000);
               }
    
               a= driver.findElements(By.cssSelector("span[jsname=\"bN97Pc\"]"));
               System.out.println("size of b is "+ a.size());
               System.out.println(a.size()== b); 
           }
    
           for (WebElement review : a) {
                System.out.println(review.getText()); 
            }