各位先进们好,近日小弟对Python有兴趣想自学,就找到一些youtuber跟网站上学习操作
目前有遇到问题的是抓取网页上的资料及输入资料。
我使用的环境是
Widnwos 10 + PyCharm + Python + Selenimu 4 + Chrome
以下为小弟的程式码,是以Dcard 为例,开启后在搜寻栏输入"餐厅",然后出来的文章标题
Print出来
========================程式码=========================================
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver =
webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
driver.get("https://www.dcard.tw/f")
search = driver.find_element(By.NAME,"query")
search.send_keys("餐厅")
search.send_keys(Keys.RETURN)
titles = driver.find_elements(By.CLASS_NAME,"sc-b57812c2-3")
for title in titles:
print(title.text)
time.sleep(5)
driver.quit()
===========================================================================
然后有问题的是有的时候执行可以完成,但是结果一直print不出东西,都是空白的。
然后有时就直接出错,如下显示:
Traceback (most recent call last):
File "C:\Users\Michael\Desktop\MyPyCharm\test.py", line 97, in <module>
driver.get("https://www.dcard.tw/f")
File
"C:\Users\Michael\Desktop\MyPyCharm\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 447, in get
self.execute(Command.GET, {'url': url})
File
"C:\Users\Michael\Desktop\MyPyCharm\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 435, in execute
self.error_handler.check_response(response)
File
"C:\Users\Michael\Desktop\MyPyCharm\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot
determine loading status
from unknown error: unexpected command response
(Session info: chrome=103.0.5060.53)
===========================================================================
在猜想应该不会是chrome webdriver的问题,感觉比较像是网页loading不完全,但又不
确定,所以想来请教一下高手们是否能为我解答是什么问题呢?
以及有什么解决办法吗?
感谢各位高手们