使用selenium,有没有办法让脚本挑选出满足特定条件的元素?
我真正想做的是让 selenium 选择拥有超过 X 个观众的所有 Twitch channel 。如果您检查元素,您会发现:
<p class="info"
562
viewers on
<a class="js-profile-link" href="/hey_jase/profile"
data-tt_content="live_channel" data-tt_content_index="1"
data-tt_medium="twitch_directory" data-ember-action="1471">
Hey_Jase
</a>
</p>
最佳答案
首先,您可以找到所有 twitch channel 链接。然后,根据观看次数过滤它们。
大致如下:
import re
from selenium import webdriver
THRESHOLD = 100
driver = webdriver.Firefox()
driver.get("url")
pattern = re.compile(r"(\d+)\s+viewers on")
for link in driver.find_elements_by_css_selector("p.info a[data-tt_content=live_channel]"):
text = link.find_element_by_xpath("..").text # get to the p parent element
match = pattern.search(text) # extract viewers count
if match:
viewers_count = int(match.group(1))
if viewers_count >= THRESHOLD:
print(link.text, viewers_count)
关于python - 使用 selenium (python) 选择满足条件的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40032560/