0
#iFrame
driver.switch_to.frame(driver.find_element_by_css_selector("#iFrameResizer0"))

#Findings elements
time.sleep(2) #Sleep to avoid 'laiks' error?
laiks = driver.find_elements_by_class_name("event-date-time__time")
time.sleep(3)
speles = driver.find_elements_by_class_name("event-block-row__name")
time.sleep(4)
odds = driver.find_elements_by_class_name("odd__value")

if laiks and speles and odds is not None:
    print("Plkst., Spēlētāji, Koeficients:")
#Create datas.txt, write into it
with open("datas.txt", "w") as output:
    for viss in laiks + speles + odds:
        #Print in console the above
        print(viss.text);
        #Write in output file
        output.write(viss.text + " | ")

这会在 data.txt 文件中生成以下内容:

23:30 | 23:30 | 22:00 | 22:30 | 23:30 | 08:00 | 11:00 | 14:00 | 16:00 | 15:00 | 18:30 | 22:00 | 15:00 | 20:00 | Havan Liberty - Isurus Gaming | Boom.id - Sharks Esports | Sector One - 4Elements Esports | Sprout - Divizon | Gamefist - Jointheforce | Touch The Crown - Unicorns of Love | Unicorns of Love - Touch The Crown | Huat Zai - After Gaming | Beyond Esports - Zigma | Tiger - Invictus Gaming | C0ntact Gaming - Mousesports | North - Sprout | Natus Vincere - Mousesports | Complexity Gaming - ENCE | BIG - Team Spirit | Astralis - Heroic | Evil Geniuses - Furia Esports | Liquid - 100 Thieves | 2.08 | 1.67 | 1.48 | 2.42 | 3.70 | 1.25 | 

然而,这不是预期的结果,也很难理解。“laiks”的第一个值表示“speles”的第一个值和前两个“odds”值。

23:30 | Havan Liberty - Isurus Gaming | 2.08 | 1.67 |
23:30 | Boom.id - Sharks Esports | 1.48 | 2.42 |

我该怎么做?

4

1 回答 1

1

您的数组似乎大小不相等,因此您必须先处理它,然后使用 for 循环将它们与您需要的分隔符拼凑在一起,并将其写入上下文中的文件。

laiks = ['23:30 ',
         ' 23:30 ',
         ' 22:00 ',
         ' 22:30 ',
         ' 23:30 ',
         ' 08:00 ',
         ' 11:00 ',
         ' 14:00 ',
         ' 16:00 ',
         ' 15:00 ',
         ' 18:30 ',
         ' 22:00 ',
         ' 15:00 ',
         ' 20:00']
speles = ['Havan Liberty - Isurus Gaming ',
          ' Boom.id - Sharks Esports ',
          ' Sector One - 4Elements Esports ',
          ' Sprout - Divizon ',
          ' Gamefist - Jointheforce ',
          ' Touch The Crown - Unicorns of Love ',
          ' Unicorns of Love - Touch The Crown ',
          ' Huat Zai - After Gaming ',
          ' Beyond Esports - Zigma ',
          ' Tiger - Invictus Gaming ',
          ' C0ntact Gaming - Mousesports ',
          ' North - Sprout ',
          ' Natus Vincere - Mousesports ',
          ' Complexity Gaming - ENCE ',
          ' BIG - Team Spirit ',
          ' Astralis - Heroic ',
          ' Evil Geniuses - Furia Esports ',
          ' Liquid - 100 Thieves ']
odds = ['2.08',
        '1.67',
        '1.48',
        '2.42',
        '3.70',
        '1.25']

# handle unequal size array
max_samples = max(len(laiks), len(speles), len(odds))
# equalize the arrays
for array in [speles, laiks, odds]:
    if len(array) < max_samples:
        array = array.extend([' '] * (max_samples - len(array)))
# piece them together
for i in range(max_samples):
    print laiks[i] + '|' + speles[i] + '|' + odds[i]
    # output.write(laiks[i] + '|' + speles[i] + '|' + odds[i])

输出

23:30 |Havan Liberty - Isurus Gaming |2.08
 23:30 | Boom.id - Sharks Esports |1.67
 22:00 | Sector One - 4Elements Esports |1.48
 22:30 | Sprout - Divizon |2.42
 23:30 | Gamefist - Jointheforce |3.70
 08:00 | Touch The Crown - Unicorns of Love |1.25
 11:00 | Unicorns of Love - Touch The Crown | 
 14:00 | Huat Zai - After Gaming | 
 16:00 | Beyond Esports - Zigma | 
 15:00 | Tiger - Invictus Gaming | 
 18:30 | C0ntact Gaming - Mousesports | 
 22:00 | North - Sprout | 
 15:00 | Natus Vincere - Mousesports | 
 20:00| Complexity Gaming - ENCE | 
 | BIG - Team Spirit | 
 | Astralis - Heroic | 
 | Evil Geniuses - Furia Esports | 
 | Liquid - 100 Thieves | 
于 2020-09-24T19:18:09.607 回答