当前位置: 首页 > 知识库问答 >
问题:

用硒向下滚动谷歌评论

鄢子平
2023-03-14

我正试图从这个链接中抓取评论:

https://www.google.com/search?q=google回顾第二次机会治疗40街

对于我使用以下代码加载页面的内容

from selenium import webdriver
import datetime
import time
import argparse
import os
import time

#Define the argument parser to read in the URL

url = "https://www.google.com/search?q=google+reviews+2nd+chance+treatment+40th+street&rlz=1C1JZAP_enUS697US697&oq=google+reviews+2nd+chance+treatment+40th+street&aqs=chrome..69i57j69i64.6183j0j7&sourceid=chrome&ie=UTF-8#lrd=0x872b7179b68e33d5:0x24b5517d86a95f89,1"


# Initialize the Chrome webdriver and open the URL
#driver = webdriver.Chromium()


profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko")
#driver = webdriver.Firefox(profile)
# https://stackoverflow.com/questions/22476112/using-chromedriver-with-selenium-python-ubuntu
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")

driver.get(url)

driver.implicitly_wait(2)



SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

页面加载很好,它没有向下滚动,我已经对其他网站(如 linkedn)使用了相同的代码,并且它在那里工作。

共有1个答案

楚威
2023-03-14

以下是无需使用javascript向下滚动即可使用的逻辑。使用location_once_scrolled_into_view方法简单有效,该方法将滚动到元素。

作为下面逻辑的一部分,我们滚动到最后一个评论,然后检查我们是否根据请求加载了所需的评论数量。

需要进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

在下面的代码中,根据您的要求更改< code>desiredReviewsCount变量值。

wait = WebDriverWait(driver,10)
url = "https://www.google.com/search?q=google+reviews+2nd+chance+treatment+40th+street&rlz=1C1JZAP_enUS697US697&oq=google+reviews+2nd+chance+treatment+40th+street&aqs=chrome..69i57j69i64.6183j0j7&sourceid=chrome&ie=UTF-8#lrd=0x872b7179b68e33d5:0x24b5517d86a95f89,1"
driver.get(url)
x=0
desiredReviewsCount=30
wait.until(EC.presence_of_all_elements_located((By.XPATH,"//div[@class='gws-localreviews__general-reviews-block']//div[@class='WMbnJf gws-localreviews__google-review']")))
while x<desiredReviewsCount:
    driver.find_element_by_xpath("(//div[@class='gws-localreviews__general-reviews-block']//div[@class='WMbnJf gws-localreviews__google-review'])[last()]").location_once_scrolled_into_view
    x = len(driver.find_elements_by_xpath("//div[@class='gws-localreviews__general-reviews-block']//div[@class='WMbnJf gws-localreviews__google-review']"))

print (len(driver.find_elements_by_xpath("//div[@class='gws-localreviews__general-reviews-block']//div[@class='WMbnJf gws-localreviews__google-review']")))
 类似资料:
  • 即使使用上面的代码向下滚动页面,我仍然只得到10个评论。不过我没有得到任何错误。 需要帮助如何向下滚动页面,以获得至少20个评论。到目前为止,我只能得到10个评论。根据我对此问题的在线搜索,人们大多使用:“driver.execute_script(”window.scrollto(0,document.body.scrollheight);“)”若要随时向下滚动页面,请执行以下操作。但对我来说这

  • 问题内容: 我正在尝试检索具有ajax加载向下滚动功能alla Twitter的页面中的元素。由于某些原因,此功能无法正常运行。我添加了一些打印语句以对其进行调试,并且我总是得到相同数量的项目,然后函数返回。我在这里做错了什么? 问题答案: 尝试介于两者之间 注意:硬睡眠只是为了证明它有效。请改用waits包来等待智能状态。

  • 本文向大家介绍vim 向下滚动,包括了vim 向下滚动的使用技巧和注意事项,需要的朋友参考一下 示例 命令 描述 Ctrl+E 向下滚动一行。 Ctrl+D 向下滚动半屏(可使用该scroll选项配置)。 Ctrl+F 向下滚动全屏。 z+ 在窗口顶部的窗口下方绘制第一行。

  • 作为一个项目的一部分,我正试图从谷歌那里获取完整的评论(在其他网站上的之前尝试中,我的评论被一个截断,除非你点击它,否则它会隐藏完整的评论)。 我已经为此选择了rvest软件包。然而,我似乎没有得到我想要的结果。 这是我的步骤 然而,这似乎不起作用。我这里没有任何输出。 我对这个软件包和网页抓取非常陌生,所以对此的任何意见都将不胜感激。

  • 是否可以在Python中使用chromedriver和selenium对元素进行截图。我知道chrome开发工具允许我们对特定元素进行截图。即使是JavaScript方法,我也可以使用命令获取数据URI。

  • 我是新来的,我拼命地试图用SeleniumChromewebDrive在youtube上上传视频,但是当我试图登录我的谷歌账户时,它阻止了我这样做,因为Chrome意识到这个过程是自动的。它给我的错误是浏览器不安全。 有没有人知道如何避开这个问题,或者如何在我的帐户登录后打开chrome或其他建议?