如何停止在终端中无限打印文本?
How to stop infinite printing of text in terminal?
我正在尝试查看此 site 提供的所有房屋。为此,我使用 while 循环不断点击 show_more_listings button
以显示越来越多的结果。虽然我实现了我无法在 VS 代码终端中结束 except 语句内的文本打印。我尝试在 try 和 except 块中使用 break,但这样做不允许我解析整个列表,这意味着按钮只被单击一次,然后中断或停止。我还尝试找到停止循环的条件,例如 NoSuchElementException
以及在 try 块中嵌套,但 none 有效。这是代码:
from logging import exception
from typing import Text
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
import pandas as pd
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import csv
from selenium import webdriver
PATH = "C:/ProgramData/Anaconda3/scripts/chromedriver.exe" #always keeps chromedriver.exe inside scripts to save hours of debugging
driver =webdriver.Chrome(PATH) #preety important part
driver.get("https://www.gharghaderi.com/house-for-sale/")
driver.implicitly_wait(10)
while(True):
try:
show_more_listings = driver.find_element_by_xpath('//span[@class="show_more"]/button')
show_more_listings.click()
except:
print("you have reached end of the list no more houses to be shown") #this keeps printing infinitely in vs code terminal
#break, is not used here cause it prevents furthur clicking of show_more_listings to view more houses
在 try
中放置一个 if
块。喜欢下面
while True:
try:
nextoption = driver.find_element_by_xpath("//span[@class='show_more']/button")
if nextoption:
driver.execute_script("arguments[0].scrollIntoView(true);",nextoption)
driver.execute_script("window.scrollBy(0,-300)")
nextoption.click()
time.sleep(5)
except Exception as e:
print(e)
break
你的代码的问题是 While Loop
,它是一个无限循环。
当你通过 while(True):
时,它将继续执行并且 try-except 将不允许它中断,因为它将处理异常情况。
要解决此问题,您需要设置特定条件来打破它。
- 首先创建一个函数来检查 show_more 按钮是否可见。
代码:
def isDisplayed():
try:
driver.find_element_by_xpath('//span[@class="show_more"]/button')
except NoSuchElementException:
return False
return True
- 当
show_more
按钮可见时,我们只会 运行 while
循环,否则终止循环。
代码:
while(isDisplayed()):
try:
show_more_listings = driver.find_element_by_xpath('//span[@class="show_more"]/button')
show_more_listings.click()
time.sleep(3)
except:
print("you have reached end of the list no more houses to be shown")
注意:我必须使用 time.sleep(3)
才能加载下一个 load_more
按钮。请使用显式等待等
输出:无错误
也到达了页面底部:
我正在尝试查看此 site 提供的所有房屋。为此,我使用 while 循环不断点击 show_more_listings button
以显示越来越多的结果。虽然我实现了我无法在 VS 代码终端中结束 except 语句内的文本打印。我尝试在 try 和 except 块中使用 break,但这样做不允许我解析整个列表,这意味着按钮只被单击一次,然后中断或停止。我还尝试找到停止循环的条件,例如 NoSuchElementException
以及在 try 块中嵌套,但 none 有效。这是代码:
from logging import exception
from typing import Text
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
import pandas as pd
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import csv
from selenium import webdriver
PATH = "C:/ProgramData/Anaconda3/scripts/chromedriver.exe" #always keeps chromedriver.exe inside scripts to save hours of debugging
driver =webdriver.Chrome(PATH) #preety important part
driver.get("https://www.gharghaderi.com/house-for-sale/")
driver.implicitly_wait(10)
while(True):
try:
show_more_listings = driver.find_element_by_xpath('//span[@class="show_more"]/button')
show_more_listings.click()
except:
print("you have reached end of the list no more houses to be shown") #this keeps printing infinitely in vs code terminal
#break, is not used here cause it prevents furthur clicking of show_more_listings to view more houses
在 try
中放置一个 if
块。喜欢下面
while True:
try:
nextoption = driver.find_element_by_xpath("//span[@class='show_more']/button")
if nextoption:
driver.execute_script("arguments[0].scrollIntoView(true);",nextoption)
driver.execute_script("window.scrollBy(0,-300)")
nextoption.click()
time.sleep(5)
except Exception as e:
print(e)
break
你的代码的问题是 While Loop
,它是一个无限循环。
当你通过 while(True):
时,它将继续执行并且 try-except 将不允许它中断,因为它将处理异常情况。
要解决此问题,您需要设置特定条件来打破它。
- 首先创建一个函数来检查 show_more 按钮是否可见。
代码:
def isDisplayed():
try:
driver.find_element_by_xpath('//span[@class="show_more"]/button')
except NoSuchElementException:
return False
return True
- 当
show_more
按钮可见时,我们只会 运行while
循环,否则终止循环。
代码:
while(isDisplayed()):
try:
show_more_listings = driver.find_element_by_xpath('//span[@class="show_more"]/button')
show_more_listings.click()
time.sleep(3)
except:
print("you have reached end of the list no more houses to be shown")
注意:我必须使用 time.sleep(3)
才能加载下一个 load_more
按钮。请使用显式等待等
输出:无错误
也到达了页面底部: