Mary
Mary

Reputation: 797

divided tag between text with ";"

how can I add ";" a separator between each text from the row? actually, I used ';' join, but it doesn't work well, it including within tag text "FRI 1", I want to separate into "FRI" and "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()

expected result:

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

Upvotes: 3

Views: 102

Answers (2)

min2bro
min2bro

Reputation: 4628

This will add semi-colon in the spaces for each line, You can further customize to remove the semicolon between the Monday;Matches and 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()

Output:

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

Upvotes: 1

Manoj Jadhav
Manoj Jadhav

Reputation: 1368

You can do this using 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

Upvotes: 1

Related Questions