如何在行中的每个文本之间添加分隔符?实际上,我使用了";"join,但是它不好用,它包括标记文本';',我想分成"FRI 1""FRI"

# -*- coding:UTF-8 -*-
import sys
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("url")
table = ['; '.join([j.text for j in i.find_elements_by_class_name('couponRow') if j.text]) for i in driver.find_elements_by_xpath('//*[@id="todds"]//div[@class="couponTable"]') if i.text]
for line in table:
    print line
driver.close()

预期结果:
Friday Matches;
FRI ;1 ; Uruguay; vs; France; Expected In Play start selling time: ; 06/07 ; 22:00 ; 4.75 ; 2.92 ; 1.78
FRI ;2 ; Brazil; vs; Belgium; Expected In Play start selling time: ; 07/07 ; 02:00 ; 1.94 ; 3.05 ; 3.70
Saturday Matches ;
SAT ;1 ; Sweden; vs; England; Expected In Play start selling time: ; 07/07 ; 22:00 ; 5.10 ; 2.95 ; 1.73
SAT ;2 ; Russia; vs; Croatia; Expected In Play start selling time: ; 08/07 ; 02:00 ; 3.85 ; 2.70 ; 2.07

最佳答案

这将在每行的空格中添加分号,您可以进一步自定义以删除星期一之间的分号;Matches和Expected;in;Play;start;selling;time:

import sys
    from selenium import webdriver
    driver = webdriver.Chrome('C:/Users/vbabu/Downloads/chromedriver_win32/chromedriver.exe')
    driver.get("url")
    for i in driver.find_elements_by_xpath('//*[@id="todds"]//div[@class="couponTable"]'):
        for j in i.find_elements_by_class_name('couponRow'):
            print(';'.join([item for item in j.text.split(' ')]))
    driver.close()

输出:
Monday;Matches
MON;41;HammarbyvsOstersunds;Expected;In;Play;start;selling;time:
10/07;01:00;1.85;3.50;3.35
Tuesday;Matches
TUE;1;FrancevsBelgium;Expected;In;Play;start;selling;time:
11/07;02:00;2.38;2.82;2.95
Wednesday;Matches
WED;1;CroatiavsEngland;Expected;In;Play;start;selling;time:
12/07;02:00;3.45;2.80;2.15

关于python - 在带有“;”的文本之间划分标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51238738/

10-12 00:27
查看更多