1

我试图让我的脚本根据网站上表行中存在的值执行特定操作。例如,如果 x 在表 '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')
4

1 回答 1

1

如果我正确理解您需要什么,您应该同时continue从代码和break底部删除,并在and块break内添加一个,这样如果您找到了您正在寻找的条件并执行了您需要的操作 - 现在打破 for环形。 像这样:ifelse

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')

但也许我不明白你的逻辑和问题

于 2021-08-02T15:54:08.647 回答