python - Python 使用 While 循环避免重复打印
问题描述
我Python Selenium
用来解析一些live data
(Bets),我用While loop
这是我的代码中的一些逻辑
def parse():
while True:
x = driver.find_element_by_xpath('//*[@id="bets-history"]/li[0]').text
print(x)
此代码有效,但输出为
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
1.54x
13.5x
13.5x
13.5x
13.5x
13.5x
13.5x
13.5x
13.5x
13.5x
13.5x
13.5x
13.5x
什么是只打印一张的最佳方法?例如像这样
1.54x
13.5x
解决方案
您可以将第二个变量 ( y
) 分配给原始变量 ( x
),然后将新值分配给x
。然后,在 if 语句中检查它们是否不相等 ( !=
)。
编码:
def parse():
x = ''
while True:
y = x
x = driver.find_element_by_xpath('//*[@id="bets-history"]/li[0]').text
if y != x:
print(x)
推荐阅读
- openssl - 验证最终用户证书和 SubCA 失败 OpenSSL
- python - dir(package) 中的 Python 包名
- makefile - Makefile:12: *** 缺少分隔符。停止。gfortran
- javascript - chrome event.clipboardData.getData('text/html') 已停止从 Excel 工作
- flutter - Flutter PhpMyAdmin 获取数据
- elasticsearch - 用于 Logstash 的 Redis 通过 TLS 输入
- html - 我对相邻的 2 个 div 有问题
- html - 如何优化此代码以用于电子邮件签名?
- python-3.x - Python 版本和 BQ API 兼容性
- excel - 从函数调用时 Excel 宏不起作用