python - 硒的网络抓取问题/获取评论
问题描述
我一直在尝试从 Dealabs 网站执行一些网络抓取。
这是示例页面:
https://www.dealabs.com/bons-plans/saneo-climatiseur-2166879
主要目标是能够获得所有评论并打印出来。
下面的示例代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
url = "https://www.dealabs.com/bons-plans/saneo-climatiseur-2166879"
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get(url)
button = WebDriverWait(driver, 2).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/main/div[4]/div[1]/div/div[1]/div[2]/button[2]/span"))
)
button.click()
comments_list = driver.find_element_by_class_name("commentList")
comments = comments_list.find_elements_by_class_name("commentList-item")
for comment in comments:
_id = comment.get_attribute("id")
author = comment.find_element_by_class_name('userInfo-username').text
content = comment.find_element_by_class_name('userHtml-content').text
timestamp = comment.find_element_by_class_name('text--color-greyShade').text
print(_id)
print(author)
print(content)
print(timestamp)
print('-' * 30)
driver.close()
事实是这样做我只能收集评价最高的评论,而不是全部。
我有点困惑。
我错过了什么吗?
提前致谢
解决方案
您可以通过发送page
参数轻松获取评论
https://www.dealabs.com/bons-plans/saneo-climatiseur-2166879?page=1
https://www.dealabs.com/bons-plans/saneo-climatiseur-2166879?page=2
https://www.dealabs.com/bons-plans/saneo-climatiseur-2166879?page=3
等等,而不是每次都单击下一步按钮。
推荐阅读
- java - 当 url 包含分号时,在 Spring 5 中获得 500 响应
- postgresql - 如何遍历表并使用列属性在 postGIS 中创建一条线
- javascript - 通过包含函数的数组反向循环,没有jQuery
- docker - 使用 VSTS CI 将简单的 Java REST API 推送到 Docker 的步骤
- windows - 通过注册表项(HKLM\...\WinLogon、Shell)以管理员身份运行应用程序
- c++ - QtWebengine 5.7.1 的性能非常慢
- python - “for”语句中的“i”变量在函数内部有效吗?
- node.js - 将 nodeenv 与 VSCode 一起使用
- c# - C# ORM - 多个表字段到单个属性
- javascript - 测试是否使用参数调用函数