在Python中实现一个修改过的do-while循环,即在循环结束时至少执行一次和另一次?
我在实现等同于do-while循环的东西时遇到问题 问题描述 我正在抓取一个网站,结果页面已分页,即在Python中实现一个修改过的do-while循环,即在循环结束时至少执行一次和另一次?,python,loops,selenium,for-loop,while-loop,Python,Loops,Selenium,For Loop,While Loop,我在实现等同于do-while循环的东西时遇到问题 问题描述 我正在抓取一个网站,结果页面已分页,即 1, 2, 3, 4, 5, .... NEXT 我正在使用NEXT链接存在性的测试条件遍历页面。如果有一个结果页面,那么就没有NEXT链接,所以我将只刮去第一个页面。如果有多个页面,最后一个页面也没有NEXT链接。因此,scraper功能也可以在该页面上工作。刮片功能被称为findRecords() 因此,我使用以下方法隔离我的下一个链接: next_link = driver.find_e
1, 2, 3, 4, 5, .... NEXT
我正在使用NEXT
链接存在性的测试条件遍历页面。如果有一个结果页面,那么就没有NEXT
链接,所以我将只刮去第一个页面。如果有多个页面,最后一个页面也没有NEXT
链接。因此,scraper功能也可以在该页面上工作。刮片功能被称为findRecords()
因此,我使用以下方法隔离我的下一个链接:
next_link = driver.find_element(By.XPATH, "//a[contains(text(),'Next')][@style='text-decoration:underline; cursor: pointer;']")
所以我想运行一个循环,至少执行一次刮取(当有一个或多个结果页时)。我还使用click()函数单击了NEXT
按钮。到目前为止,我掌握的代码是:
while True:
findRecords()
next_link = driver.find_element(By.XPATH, "//a[contains(text(),'Next')][@style='text-decoration:underline; cursor: pointer;']")
if not next_link:
break
next_link.click()
这是行不通的。好吧,它工作正常,但当它到达最后一页时,它会给我一个NoTouchElementException
,如下所示:
回溯(最近一次呼叫最后一次):
文件“try.py”,第47行,在
next_link=driver.find_元素(By.XPATH,“//a[contains(text(),'next')][@style='text-decoration:underline;cursor:pointer;']))
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webdriver.py”,第752行,在find_元素中
'value':value})['value']
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webdriver.py”,第236行,在execute中
self.error\u handler.check\u响应(响应)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/errorhandler.py”,第192行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“//a[contains(text(),'Next')][@style='text-decoration:underline;游标:指针;']”}
(会话信息:chrome=53.0.2785.89)
(驱动程序信息:chromedriver=2.20.353124(035346203162d32c80f1dce587c8154a1efa0c3b),平台=Linux 3.13.0-92-generic x86_64)
我知道元素确实不存在于最后一页上,因为正如我前面所说的,NEXT
元素不存在于最后一页上
那么,我如何修复我的while循环,以便在条件不正确时能够刮取单个页面结果和/或最后一页,并且优雅地打破while循环,而不给我那个可怕的错误呢
PS:除了上面的while循环之外,我还尝试了以下方法:
is_continue = True
while is_continue:
findRecords()
next_link = driver.find_element(By.XPATH, "//a[contains(text(),'Next')][@style='text-decoration:underline; cursor: pointer;']")
if next_link:
is_continue = True
next_link.click()
else:
is_continue = False
如果有任何帮助,下面是我的scraper函数findRecords()
:
def findRecords():
filename = "sam_" + letter + ".csv"
bsObj = BeautifulSoup(driver.page_source, "html.parser")
tableList = bsObj.find_all("table", {"class":"width100 menu_header_top_emr"})
tdList = bsObj.find_all("td", {"class":"menu_header width100"})
for table,td in zip(tableList,tdList):
a = table.find_all("span", {"class":"results_body_text"})
b = td.find_all("span", {"class":"results_body_text"})
with open(filename, "a") as csv_file:
csv_file.write(', '.join(tag.get_text().strip() for tag in a+b) +'\n')
搜索下一个链接时,请更改代码以查找_元素,如果存在下一个链接,则返回大小为1的列表,否则返回大小为0的列表,但无例外
next_link = driver.find_elements(By.XPATH, "//a[contains(text(),'Next')][@style='text-decoration:underline; cursor: pointer;']")
您现在需要设置逻辑来访问此列表中的下一个webelement。当您搜索下一个链接时,更改代码以查找元素,如果存在下一个,则返回大小为1的列表,否则返回大小为0的列表,但无例外
next_link = driver.find_elements(By.XPATH, "//a[contains(text(),'Next')][@style='text-decoration:underline; cursor: pointer;']")
您现在需要设置逻辑来访问此列表中的下一个webelement。您应该尝试使用find\u elements
,它将返回webelement列表或空列表。因此,只需检查其长度,如下所示:-
while True:
findRecords()
next_link = driver.find_elements(By.XPATH, "//a[contains(text(),'Next')][@style='text-decoration:underline; cursor: pointer;']")
if len(next_link) == 0:
break
next_link[0].click()
您应该尝试使用find_elements
,它将返回WebElement列表或空列表。因此,只需检查其长度,如下所示:-
while True:
findRecords()
next_link = driver.find_elements(By.XPATH, "//a[contains(text(),'Next')][@style='text-decoration:underline; cursor: pointer;']")
if len(next_link) == 0:
break
next_link[0].click()
修复缩进,并向我们显示完整的堆栈跟踪。@user2357112我已经修复了两者,以匹配我所拥有的。修复缩进,并向我们显示完整的堆栈跟踪。@user2357112我已经修复了两者,以匹配我所拥有的。