【发布时间】:2016-11-01 13:54:26
【问题描述】:
我使用 Python Selenium 来抓取网站, 但是我的爬虫因为异常停止了:
StaleElementReferenceException: Message: stale element reference: element is not attach to the page document
即使没有附加元素,我如何继续抓取?
更新
我将代码更改为:
try:
libelle1 = prod.find_element_by_css_selector('.em11')
libelle1produit = libelle1.text # libelle1 OK
libelle1produit = libelle1produit.decode('utf-8', 'strict')
except StaleElementReferenceException:
pass
但我有这个例外
NoSuchElementException: Message: no such element
我也试过这个:
try:
libelle1 = prod.find_element_by_css_selector('.em11')
libelle1produit = libelle1.text # libelle1 OK
libelle1produit = libelle1produit.decode('utf-8', 'strict')
except :
pass
【问题讨论】:
-
考虑发布更多导致问题的代码。否则,下一个最好的方法就是使用 try-except 或
with supress上下文管理器忽略异常
标签: python selenium selenium-webdriver web-scraping web-crawler