Python 2.7 BeautifulSoup在表上迭代并将每行值插入列表变量TypeError:列表索引必须是整数,而不是标记

Python 2.7 BeautifulSoup在表上迭代并将每行值插入列表变量TypeError:列表索引必须是整数,而不是标记,python-2.7,beautifulsoup,Python 2.7,Beautifulsoup,我在for循环中对一个表进行迭代,并希望将该值存储在一个列表变量中。如果我把它存储到一个变量中,当我在函数调用中返回值时,我只会得到第一个值。 在for循环的每次迭代中,我都有几个值。将其存储在变量中是不好的。我需要将其存储到一个列表中,以便捕获所有值 我得到的错误是: list1[div] = div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8') TypeError: list ind

我在for循环中对一个表进行迭代,并希望将该值存储在一个列表变量中。如果我把它存储到一个变量中,当我在函数调用中返回值时,我只会得到第一个值。 在for循环的每次迭代中,我都有几个值。将其存储在变量中是不好的。我需要将其存储到一个列表中,以便捕获所有值

我得到的错误是:

list1[div] = div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')
TypeError: list indices must be integers, not Tag
我的代码是:

def extract_testcases_from_report_htmltestrunner():
filename = (r"C:\temp\selenium_report\ClearCore501_Automated_GUI_TestReport.html")
html_report_part = open(filename,'r')
soup = BeautifulSoup(html_report_part, "html.parser")

for div in soup.select("#result_table tr div.testcase"):
    print(div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8'))
    list1 = []
    for div in soup.select("#result_table tr div.testcase"):
        var = div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')
        list1[div] = div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')
        return var
如果我注释掉return var,那么print语句的输出是:

('test_000001_login_valid_user', 'pass')
('test_000002_select_a_project', 'pass')
('test_000003_verify_Lademo_CRM_DataPreview_is_present', 'pass')
('test_000004_view_data_preview_Lademo_CRM_and_test_scrollpage', 'pass')
('test_000005_sort_data_preview_by_selecting_column_header', 'pass')
# etc.  More tests
如果我使用返回变量调用函数,我的输出是:

('test_000001_login_valid_user', 'pass')
[('test_000001_login_valid_user', 'pass'), ('test_000002_select_a_project', 'pass'), ('test_000003_verify_Lademo_CRM_DataPreview_is_present', 'pass') etc.
我希望我的函数调用返回所有测试用例。我想我需要把它作为一个列表返回。然后,我可以调用此函数并迭代列表,并将其打印到电子邮件的电子邮件代码中

谢谢,里亚兹

我现在把它作为一个列表返回。当我调用函数并打印它的返回值时,它会打印一行中的所有值。我想把它分成几行

def extract_testcases_from_report_htmltestrunner():
    filename = (r"C:\temp\selenium_report\ClearCore501_Automated_GUI_TestReport.html")
    html_report_part = open(filename,'r')
    soup = BeautifulSoup(html_report_part, "html.parser")

    list1 = []
    for div in soup.select("#result_table tr div.testcase"):
        #print(div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8'))
        list1.append((div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')))
    return list1

if __name__ == "__main__":

    print extract_testcases_from_report_htmltestrunner()
输出为:

('test_000001_login_valid_user', 'pass')
[('test_000001_login_valid_user', 'pass'), ('test_000002_select_a_project', 'pass'), ('test_000003_verify_Lademo_CRM_DataPreview_is_present', 'pass') etc.
我希望输出如下:

[('test_000001_login_valid_user', 'pass')
('test_000002_select_a_project', 'pass')
('test_000003_verify_Lademo_CRM_DataPreview_is_present', 'pass')
etc.  
谢谢,Riaz

如果您愿意,您只能从函数返回一次,这样您的函数在第一次迭代中达到返回值时就结束了:

from bs4 import BeautifulSoup
def extract_testcases_from_report_htmltestrunner():
    filename = (r"C:\temp\selenium_report\ClearCore501_Automated_GUI_TestReport.html")
    html_report_part = open(filename,'r')
    soup = BeautifulSoup(html_report_part, "html.parser")
    for div in soup.select("#result_table tr div.testcase"):
          yield div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')


all_data = list(extract_testcases_from_report_htmltestrunner())
如果您希望,您只能从函数返回一次,这样,您的函数在第一次迭代中达到返回值时即结束:

from bs4 import BeautifulSoup
def extract_testcases_from_report_htmltestrunner():
    filename = (r"C:\temp\selenium_report\ClearCore501_Automated_GUI_TestReport.html")
    html_report_part = open(filename,'r')
    soup = BeautifulSoup(html_report_part, "html.parser")
    for div in soup.select("#result_table tr div.testcase"):
          yield div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')


all_data = list(extract_testcases_from_report_htmltestrunner())

我不知道如何使用这个功能。如果我调用函数If name==“main”:打印从报告中提取标题我得到的输出是,我试图在函数return all_data的末尾加上一个return,它说return在这个函数之外。我希望函数返回所有的值,这样当我调用函数时,我就可以将所有的值都放在邮件正文部分的邮件代码中。谢谢,我现在拿到了。如果name==“main”:all_data=list(从报告中提取测试用例)打印所有数据非常感谢您的帮助。我不确定如何使用此函数。如果我调用函数If name==“main”:打印从报告中提取标题我得到的输出是,我试图在函数return all_data的末尾加上一个return,它说return在这个函数之外。我希望函数返回所有的值,这样当我调用函数时,我就可以将所有的值都放在邮件正文部分的邮件代码中。谢谢,我现在拿到了。如果name==“main”:all_data=list(从报告中提取测试用例)打印所有数据非常感谢您的帮助。