简体   繁体   English

在 python 中使用循环抓取时,如何将每次迭代的 output 导出到 csv

[英]How to export to csv the output of every iteration when scraping with a loop in python

I am very new to Python.我对 Python 很陌生。 I am trying to scrape a website and I have created a small code for this:我正在尝试抓取一个网站,并为此创建了一个小代码:

select = Select(character.find_element_by_id('character-template-choice'))
options = select.options
for index in range(0, len(options) - 0):
    select.select_by_index(index)
    option1 = character.find_elements_by_class_name('pc-stat-value')
    power = []
    for c in option1:
        power.append("Power:" + c.text)
    option2 = character.find_elements_by_class_name(
        'unit-stat-group-stat-label')
    skills = []
    for a in option2:
        skills.append(a.text)
    option3 = character.find_elements_by_class_name(
        'unit-stat-group-stat-value')
    values = []
    for b in option3:
        values.append(b.text)
 test = [power, skills, values]
 print(test)
 df = pd.DataFrame(test).T
 df.to_csv('test2.csv', index=False, header=False)

The issue I have is when I try to export "test" list of lists to csv I only get the last iteration.我遇到的问题是,当我尝试将列表的“测试”列表导出到 csv 时,我只得到最后一次迭代。 I want to get the data for every iteration but I don't know how to do so.我想获取每次迭代的数据,但我不知道该怎么做。 Can someone help me?有人能帮我吗?

Thank you谢谢

You will need to append these three list every iteration to get then all in the end:您将需要 append 这三个列表每次迭代都得到然后全部结束:

select = Select(character.find_element_by_id('character-template-choice'))
options = select.options
test = []  # Create empty list
for index in range(0, len(options) - 0):
    select.select_by_index(index)
    option1 = character.find_elements_by_class_name('pc-stat-value')
    power = []
    for c in option1:
        power.append("Power:" + c.text)
    option2 = character.find_elements_by_class_name(
        'unit-stat-group-stat-label')
    skills = []
    for a in option2:
        skills.append(a.text)
    option3 = character.find_elements_by_class_name(
        'unit-stat-group-stat-value')
    values = []
    for b in option3:
        values.append(b.text)
    test.append([power, skills, values])  # Add a list of lists to your test

print(test)
df = pd.DataFrame(test).T
df.to_csv('test2.csv', index=False, header=False)

This will give you a list test with power, skills, values inside one list for iteration.这将为您提供一个列表test ,其中包含一个列表中的power, skills, values以进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM