在带有“;”的文本之间划分标签

时间:2018-07-09 05:32:45

标签: python selenium-webdriver xpath webdriver

如何在行中的每个文本之间添加";"分隔符?实际上,我使用了';'连接,但是效果不佳,它包含在标记文本"FRI 1"中,我想分为"FRI""1"

# -*- 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

2 个答案:

答案 0 :(得分:1)

您可以使用split

# -*- coding:UTF-8 -*-
import sys
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://bet.hkjc.com/football/odds/odds_inplay.aspx?lang=EN")
table = ['; '.join(["; ".join( j.text.split(" ")) 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()
enter code here

答案 1 :(得分:1)

这将在每行的空格中添加分号,您可以进一步自定义以删除星期一;比赛和期望;进入;播放;开始;销售;时间:

之间的分号。

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