在 python 中使用 selenium 查找网站上多行的 table 中是否存在特定值的最简单方法是什么?
what is the easiest way to find if a specific value exists in a table with multiple rows on a website using selenium in python?
我试图让我的脚本根据网站上 table 行中存在的值执行特定操作。例如,如果 x 在 table 'lab' 的第 1 行中,则创建调查,否则移至下一行并检查 x 是否在该行中。抱歉,没有帐户的人无法访问我正在尝试的网站,但请查看我的代码的更简单版本以帮助解决这个问题。截至目前,我被困在第二个 for 循环中,下面的代码遍历每一行并将其打印出来但只是挂起。可能只是我需要的休息时间,但我已经尝试了所有方法(休息、继续、通过)。
#for each patient id in list, find the section in the patients web-table by looking through each row where the covid name and result is stored
#search for results table within a table in a web page for eicrs
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
continue #continue loop through rows
continue #not sure if needed
break #break out of topmost for loop and move to next line of code once value has been found to match condition.
print('done with that')
如果我理解正确你需要什么,你应该从代码中删除 continue
和底部的 break
并在 [=14] 中添加一个 break
=] 和 else
块,所以如果您找到了您正在寻找的条件并执行了您需要的操作 - 现在打破 for 循环。
像这样:
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
break
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
break
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
has been found to match condition.
print('done with that')
不过可能我不明白你的逻辑和问题
我试图让我的脚本根据网站上 table 行中存在的值执行特定操作。例如,如果 x 在 table 'lab' 的第 1 行中,则创建调查,否则移至下一行并检查 x 是否在该行中。抱歉,没有帐户的人无法访问我正在尝试的网站,但请查看我的代码的更简单版本以帮助解决这个问题。截至目前,我被困在第二个 for 循环中,下面的代码遍历每一行并将其打印出来但只是挂起。可能只是我需要的休息时间,但我已经尝试了所有方法(休息、继续、通过)。
#for each patient id in list, find the section in the patients web-table by looking through each row where the covid name and result is stored
#search for results table within a table in a web page for eicrs
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
continue #continue loop through rows
continue #not sure if needed
break #break out of topmost for loop and move to next line of code once value has been found to match condition.
print('done with that')
如果我理解正确你需要什么,你应该从代码中删除 continue
和底部的 break
并在 [=14] 中添加一个 break
=] 和 else
块,所以如果您找到了您正在寻找的条件并执行了您需要的操作 - 现在打破 for 循环。
像这样:
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
break
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
break
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
has been found to match condition.
print('done with that')
不过可能我不明白你的逻辑和问题