【问题标题】:Web scraping using Selenium and BeautifulSoup does not update extracted code after scrolling使用 Selenium 和 BeautifulSoup 进行网页抓取不会在滚动后更新提取的代码
【发布时间】:2019-05-06 22:02:11
【问题描述】:

我正在尝试搜集 Steam 上一些游戏的评论。除非您滚动到页面底部,否则评论页面上只有 10 条评论可用,并且将加载更多评论。 我使用 selenium 进行滚动,但预计包含 20 条评论的 BeautifulSoup 对象仍然只有 10 条。 这是我的代码:

from bs4 import BeautifulSoup
from selenium import webdriver
import time

driver = webdriver.Chrome('E:\Download\chromedriver.exe')
driver.get('https://steamcommunity.com/app/466560/reviews/?browsefilter=toprated&snr=1_5_100010_')
SCROLL_PAUSE_TIME = 0.5
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
soup = BeautifulSoup(driver.page_source)

我该如何解决?

【问题讨论】:

  • 我做到了,我检查了 len(soup.text) 并且每次向下滚动并获得新汤时都会不断增加
  • @IslamTaha 我不太明白。 “每次我向下滚动”是什么意思?
  • driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  • 它实际上已经更新了。真的很奇怪,因为 30 分钟前我尝试打印汤时它保持不变。

标签: python selenium-webdriver web-scraping beautifulsoup selenium-chromedriver


【解决方案1】:

您需要等到元素 ID action_wait 不可见并在没有更多评论时查找文本,或者只需设置您想要的最大评论。

在本例中,结果限制为 100,您可以增加它,但如果您不想等待更长时间,只需 Ctrl + C,数据将被处理到 beautifulsoup。

driver.get('https://.....')
maxResult = 100
currentResults = 0
pageSource = ''

try:
    print('press "Ctrl + C" to stop loop and process using beautfulsoup.')
    while currentResults < maxResult:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.ID, "action_wait")))
        currentResults = len(driver.find_elements_by_css_selector('.apphub_Card.modalContentLink.interactable'))
        print('currentResults: %s' % currentResults)
        pageSource = driver.page_source
except KeyboardInterrupt:
        print "Cancelled by user"
except: pass

soup = BeautifulSoup(pageSource, 'html.parser')

reviews = soup.select('.apphub_Card.modalContentLink.interactable')

print('reviews count by BeautifulSoup: %s' % len(reviews))

【讨论】:

    【解决方案2】:

    页面使用 jquery 更新,每次滚动 10 条记录。它每次偏移以获得下一组。列表用完时可以看到文本,您可以使用它滚动到最后。如果您想在任何特定点停止,请将循环退出条件设置为 len(d.find_elements_by_css_selector('.reviewInfo')) 给出的所需评论数

    from selenium import webdriver
    
    d  = webdriver.Chrome()
    url = 'https://steamcommunity.com/app/466560/reviews/?browsefilter=toprated&snr=1_5_100010_'
    d.get(url)
    
    while d.find_element_by_css_selector('.apphub_NoMoreContentText1').text != 'No more content. So sad.':
        d.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        try: 
            d.find_element_by_id('GetMoreContentBtn').click()
        except:
            pass
    print(len(d.find_elements_by_css_selector('.reviewInfo')))  #6135
    

    【讨论】:

      猜你喜欢
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 2021-04-28
      • 2018-11-08
      • 2018-08-02
      • 1970-01-01
      • 2021-09-10
      相关资源
      最近更新 更多