Python Selenium使用循环检查关键字


keywords = ['no stock','out of stock','not available']
n = 0
while True:
n+=1
print(f'now check {n} times')
for keyword in keywords:
if keyword in driver.page_source:
print(f'found {keyword}, refresh after 30 seconds')
time.sleep(30)
driver.get(url)
else:
print(f'could not find any of keyword')
break

大家好,希望大家都过得好。

我正在尝试使用硒,并检查一个项目是否在网页中可用。我的想法是把所有与没有股票相关的关键字放在一个列表中,每30秒循环一次检查。

但是,如果在上加一个break,代码只扫描list中的第一个项目。有没有办法打破循环,如果所有三个关键字都不在网站上?

谢谢你的帮助。

也许这是你正在寻找的逻辑?

keywords = ['no stock','out of stock','not available']
n = 0
while True:
n+=1
print(f'now check {n} times')
keywords_found = 0
for keyword in keywords:
if keyword in driver.page_source:
keywords_found += 1
print(f'found {keyword}, {keywords_found} of {len(keywords)}')
if keywords_found == len(keywords):
print(f"We found the {len(keywords)} items of the list intot he page, so sleep and navigate")
time.sleep(30)
driver.get(url)
else:
print(f'could not the {len(keywords)} keywords, break')
break

最新更新