Traceback (most recent call last): File "C:\Users\niuxi\pythonProject\main.py", line 14, in <module> total_reviews = edge.find_element("css selector", ".rev-total a").text File "D:\元气壁纸缓存\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 740, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] File "D:\元气壁纸缓存\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute self.error_handler.check_response(response) File "D:\元气壁纸缓存\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".rev-total a"} (Session info: MicrosoftEdge=114.0.1823.79); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception Stacktrace: Backtrace: GetHandleVerifier [0x00007FF6E502AEC2+64226] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6E4FBC082+765362] (No symbol) [0x00007FF6E4D7CC4C] (No symbol) [0x00007FF6E4DC0BDF] (No symbol) [0x00007FF6E4DC0D6A] (No symbol) [0x00007FF6E4DFAC17] (No symbol) [0x00007FF6E4DDF03F] (No symbol) [0x00007FF6E4DB4BB1] (No symbol) [0x00007FF6E4DF7FC1] (No symbol) [0x00007FF6E4DDEDD3] (No symbol) [0x00007FF6E4DB3BEC] (No symbol) [0x00007FF6E4DB2DD6] (No symbol) [0x00007FF6E4DB4364] Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF6E52087C9+1319033] (No symbol) [0x00007FF6E4E2D2A8] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6E4F095E1+33553] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6E4F01A1F+1871] Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF6E52073E3+1313939] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6E4FC46B8+20232] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6E4FC0CD4+5412] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6E4FC0DCC+5660] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6E4FB4A81+735153] BaseThreadInitThunk [0x00007FFEC68526AD+29] RtlUserThreadStart [0x00007FFEC802AA68+40]
时间: 2024-04-18 07:30:54 浏览: 225
Mac下anaconda中Spyder打开报错Traceback(most recent call last)…问题
5星 · 资源好评率100%
抱歉,之前给出的代码存在一些问题。由于网站结构的变化,导致之前的选择器无法找到元素。
请尝试使用以下修改后的代码:
```python
import time
import openpyxl
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
url = "https://www.mafengwo.cn/poi/16790.html"
edge = webdriver.Edge()
edge.get(url)
comments_data = []
# 等待页面加载完成
time.sleep(5)
# 点击展开更多评论按钮
more_btn = WebDriverWait(edge, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".rev-more a")))
edge.execute_script("arguments[0].click();", more_btn)
time.sleep(2)
# 获取总评数、好评数、中评数、差评数
total_reviews = edge.find_element("css selector", ".rev-total .total").text
good_reviews = edge.find_element("css selector", ".rev-total .good").text
average_reviews = edge.find_element("css selector", ".rev-total .average").text
bad_reviews = edge.find_element("css selector", ".rev-total .bad").text
print("总评数:", total_reviews)
print("好评数:", good_reviews)
print("中评数:", average_reviews)
print("差评数:", bad_reviews)
# 循环获取点评信息
while True:
# 获取页面源码
html = edge.page_source
soup = BeautifulSoup(html, "html.parser")
# 获取每条点评的信息
reviews = soup.select(".rev-item")
for review in reviews:
# 获取点评内容
content = review.select_one(".rev-txt").get_text().strip()
print("点评内容:", content)
# 获取点评时间
date = review.select_one(".time").get_text().strip()
print("点评时间:", date)
# 获取点评用户
user = review.select_one(".name").get_text().strip()
print("点评用户:", user)
# 获取点评评分
score = review.select_one(".score span").get_text().strip()
print("点评评分:", score)
print("-----------------------------")
comments_data.append([content, date, user, score])
# 翻页操作
next_btn = edge.find_element("css selector", ".pg-next")
if next_btn.get_attribute("class") == "pg-next":
edge.execute_script("arguments[0].click();", next_btn)
time.sleep(2)
else:
break
# 将爬取的点评信息存入comments.xlsx文件中
wb = openpyxl.Workbook()
ws = wb.active
ws.append(["点评内容", "点评时间", "点评用户", "点评评分"])
for data in comments_data:
ws.append(data)
wb.save("comments.xlsx")
wb.close()
# 关闭浏览器
edge.quit()
```
在这个修改后的代码中,我更新了选择器来查找展开更多评论按钮,并且使用新的选择器来获取总评数、好评数、中评数和差评数。
希望这次修改能解决问题。如果您有任何其他问题,请随时提问。
阅读全文