我正在尝试用python中的selenium来获取google评论。我已经从selenium python模块导入了webdriver。然后我已经将self.driver初始化如下:
self.driver = webdriver.Chrome(executable_path="./chromedriver.exe",chrome_options=webdriver.ChromeOptions())
在这之后,我使用以下代码在google主页上输入我需要的评论的公司名称,现在我正在尝试获取“斯坦利桥牌循环和运动有限公司”的评论:
company_name = self.driver.find_element_by_name("q")
company_name.send_keys("STANLEY BRIDGE CYCLES AND SPORTS LIMITED ")
time.sleep(2)
然后点击google搜索按钮,使用下面的代码:
self.driver.find_element_by_name("btnK").click()
time.sleep(2)
然后我终于到了可以看到结果的页面。现在我想点击查看google评论按钮。为此,使用以下代码:
self.driver.find_elements_by_link_text("View all Google reviews")[0].click()
time.sleep(2)
现在我可以得到评论,但只有10条。我需要一个公司至少20条评论。为此,我尝试使用以下代码向下滚动页面:self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(5)
即使使用上面的代码向下滚动页面,我仍然只能得到10条评论。不过,我没有收到任何错误。
需要关于如何向下滚动页面以获得至少20条评论的帮助。到目前为止,我只能得到10条评论。根据我对这个问题的在线搜索,人们通常使用:"driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")“来在需要的时候向下滚动页面。但对我来说,这是行不通的。我检查了页面前后的高度("driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")")是否相同。
发布于 2018-12-12 20:05:20
使用Javascript滚动到最后一次审阅,这将触发额外的审阅加载。
last_review = self.driver.find_element_by_css_selector('div.gws-localreviews__google-review:last-of-type')
self.driver.execute_script('arguments[0].scrollIntoView(true);', last_review)
编辑:
下面的示例在Firefox和Chrome上运行正常,您可以根据需要重用提取google评论功能
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
def extract_google_reviews(driver, query):
driver.get('https://www.google.com/?hl=en')
driver.find_element_by_name('q').send_keys(query)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.NAME, 'btnK'))).click()
reviews_header = driver.find_element_by_css_selector('div.kp-header')
reviews_link = reviews_header.find_element_by_partial_link_text('Google reviews')
number_of_reviews = int(reviews_link.text.split()[0])
reviews_link.click()
all_reviews = WebDriverWait(driver, 3).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'div.gws-localreviews__google-review')))
while len(all_reviews) < number_of_reviews:
driver.execute_script('arguments[0].scrollIntoView(true);', all_reviews[-1])
WebDriverWait(driver, 5, 0.25).until_not(EC.presence_of_element_located((By.CSS_SELECTOR, 'div[class$="activityIndicator"]')))
all_reviews = driver.find_elements_by_css_selector('div.gws-localreviews__google-review')
reviews = []
for review in all_reviews:
try:
full_text_element = review.find_element_by_css_selector('span.review-full-text')
except NoSuchElementException:
full_text_element = review.find_element_by_css_selector('span[class^="r-"]')
reviews.append(full_text_element.get_attribute('textContent'))
return reviews
if __name__ == '__main__':
try:
driver = webdriver.Firefox()
reviews = extract_google_reviews(driver, 'STANLEY BRIDGE CYCLES AND SPORTS LIMITED')
finally:
driver.quit()
print(reviews)
发布于 2018-12-12 19:25:16
lenOfPage = driver.execute_script('window.scrollTo(0, [hard code the height])')
对我来说,如果我一遍又一遍地为同一页使用这个自动化测试,我会硬编码高度。
或者,您可以让它不断循环以向下滚动页面,直到找到元素为止。
发布于 2021-12-27 05:23:56
请共享您的URL页面。我刚刚检查过了,scrollTo工作正常。
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
或者,您可以平滑地滚动。
self.driver.execute_script('window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });')
https://stackoverflow.com/questions/53749984
复制相似问题