【问题标题】:Python: Loading all web content in seleniumPython:在硒中加载所有网页内容
【发布时间】:2020-05-13 16:33:00
【问题描述】:

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

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

上述命令不会加载所有审阅者 cmets。我的意思是它只加载前 39 个 cmets,而不加载剩余的 cmets。有什么方法可以一次性加载所有 cmets?

【问题讨论】:

  • 添加一些滚动效果如何?

标签: python selenium-webdriver beautifulsoup


【解决方案1】:

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

试试这个:

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

您可能必须这样做几次才能加载所有数据

【讨论】:

    【解决方案2】:

    由于延迟加载,您可以使用无限循环并加载页面直到找到Show More 元素。为了减慢循环我使用了time.sleep(1)。它在该页面上提供了 200 条评论。如果您想获得更多评论,则需要再次点击 Show More

    但是有些评论格式不支持,因此请尝试..except 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多