我想使用Python在Google Play商店中查看并获得所有评论。但是,我必须单击“查看更多”按钮。我想我需要循环。
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
SLEEP=5
def pause():
"""pause"""
time.sleep(SLEEP)
options = webdriver.ChromeOptions()
options.add_argument('window-size=1920x2080')
browser = webdriver.Chrome('C:/chromedriver.exe', chrome_options=options)
browser.get("https://play.google.com/store/apps/detailsid=com.spotify.music&hl=us&showAllReviews=true")
time.sleep(1)
pause()
browser.execute_script("window.scrollBy(0,5500)")
while True:
button = WebDriverWait(browser,60).until(EC.presence_of_element_located((By.XPATH, """""")))
button.click()
答案 0 :(得分:0)
注意
execute_script(脚本,* args)- 在当前窗口/框架中同步执行JavaScript。
所以您只需
和代码
lastHeight = browser.execute_script("return document.body.scrollHeight")
while true:
browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")
time.sleep(1)
newHeight = browser.execute_script("return document.body.scrollHeight")
if newHeight != lastHeight:
#parse new data
else:
#button click
#parse new data
lastHeight = newHeight