Python:在selenium中加载所有web内容

2024-05-03 15:21:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用selenium和beautifulsoup检索特定应用程序(https://play.google.com/store/apps/details?id=com.getsomeheadspace.android&hl=en&showAllReviews=true)的所有审阅者评论。我使用以下代码加载链接:

driver = webdriver.Chrome(path)
driver.get('https://play.google.com/store/apps/details?id=com.tudasoft.android.BeMakeup&hl=en&showAllReviews=true')

上述命令不会加载所有审阅者的注释。我的意思是它只加载前39条评论,不加载其余的评论。是否有任何方法可以一次性加载所有注释


Tags: appsstorehttpscomidtrueplaydriver
2条回答

看起来您必须向下滚动才能获得页面上的所有信息

试试这个:

driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")

您可能需要多次这样做才能加载所有数据

您可以使用无限循环并加载页面,直到由于延迟加载而找到Show More元素。为了降低循环速度,我使用了time.sleep(1)。该页面上有200条评论。如果你想获得更多评论,你需要再次点击Show More

但是,一些评论格式不支持,因此请尝试。除了block。希望这会有所帮助

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

driver = webdriver.Chrome()
driver.get('https://play.google.com/store/apps/details?id=com.tudasoft.android.BeMakeup&hl=en&showAllReviews=true')

while True:
  driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
  time.sleep(1)
  elements=WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.UD7Dzf')))
  if len(driver.find_elements_by_xpath("//span[text()='Show More']"))>0:
      break;

print(len(elements))
allreview=[]
try:
   for review in elements:
       allreview.append(review.text)
except:
    allreview.append("format incorrect")

print(allreview)

相关问题 更多 >