附加到python列表

附加到python列表,python,list,Python,List,我有一个web刮板,它像下面的例子一样返回我的值 # Other code above here. test = [] results = driver.find_elements_by_css_selector("li.result_content") for result in results: # Other code about result.find_element_by_blah_blah product_feature = result.fin

我有一个web刮板,它像下面的例子一样返回我的值

# Other code above here.
test = []

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")

    for each_item in product_feature.find_elements_by_tag_name('img'):
        zz = test.append(each_item.get_attribute('src')[34:-4])  # returning the values I want
        print(zz)
上面的代码将打印出如下结果:(这是我想要的值)

我希望取得以下成果:

[TCP_active, CI, DOH_active]
[TCP_active, CI, DOH]
[TCP, CI_active, DOH_active]
我应该怎么做

我试过:

test.append(each_item.get_attribute('src')[34:-4])
但这给了我:

[TCP_active]
[TCP_active, CI]
[TCP_active, CI, DOH_active]
[TCP_active, CI, DOH_active, TCP]
...

希望我的解释清楚

而不是
打印
,将您的结果添加到列表中;外循环每次迭代一个新列表:

test = []

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")
    features = [] 
    for each_item in product_feature.find_elements_by_tag_name('img'):
        features.append(each_item.get_attribute('src')[34:-4])
    test.append(features)

如果需要,您可以打印
功能
,或者
测试
,只是为了查看
循环的
每个级别上发生了什么。

将结果附加到列表中,而不是
打印
;外循环每次迭代一个新列表:

test = []

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")
    features = [] 
    for each_item in product_feature.find_elements_by_tag_name('img'):
        features.append(each_item.get_attribute('src')[34:-4])
    test.append(features)

如果需要,您可以打印
功能
,或
测试
,只需查看
循环的
每一级发生了什么。

好的,不完全确定您想要什么,但下面的代码将给出您想要的输出:

test = []

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")

    zz = []
    for each_item in product_feature.find_elements_by_tag_name('img'):
        zz = test.append(each_item.get_attribute('src')[34:-4])  # returning the values I want

    print(zz)
如果要存储数据而不是打印数据,请使用类似以下内容的词典:

test = []
zz_store = {}

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")

    zz = []
    for each_item in product_feature.find_elements_by_tag_name('img'):
        zz = test.append(each_item.get_attribute('src')[34:-4])  # returning the values I want

    zz_store[result] = zz
    print(zz)

好的,不完全确定您想要什么,但下面的代码将给出您想要的输出:

test = []

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")

    zz = []
    for each_item in product_feature.find_elements_by_tag_name('img'):
        zz = test.append(each_item.get_attribute('src')[34:-4])  # returning the values I want

    print(zz)
如果要存储数据而不是打印数据,请使用类似以下内容的词典:

test = []
zz_store = {}

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")

    zz = []
    for each_item in product_feature.find_elements_by_tag_name('img'):
        zz = test.append(each_item.get_attribute('src')[34:-4])  # returning the values I want

    zz_store[result] = zz
    print(zz)

我不确定我是否完全理解,但你有没有考虑过用字典来解释这个问题?字典似乎是一个更好、更优雅的解决方案……修正了我的问题,这可能更有帮助为每个
结果创建一个新列表,为产品功能中的每个项目创建一个
迭代。然后将该列表附加到
测试中
。本打算发布答案,但@MartijnPieters的想法是正确的,所以我会给他一个机会。我不确定我是否完全理解,但你有没有考虑过使用字典?字典似乎是一个更好、更优雅的解决方案……修正了我的问题,这可能更有帮助为每个
结果创建一个新列表,为产品功能中的每个项目创建一个
迭代。然后将该列表附加到
test
。本打算发布答案,但@MartijnPieters有正确的想法,所以我会给他一个机会。感谢您的快速回复!我试过你的方法,效果很好。:)谢谢你的快速回复!我试过你的方法,效果很好。:)