python - 在带有 ";"的文本之间划分标签

标签 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

最佳答案

这将在每行的空格中添加分号,您可以进一步自定义以删除 Monday;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/

相关文章:

python - 使用python搜索和替换xml/文本文件中的多行

python - 导入错误 : cannot import name 'ClassVar' after installing airflow

python - 如何将元素附加到 DataFrame 中的列表?

javascript - 如何配置 Protractor (JS) 以在 Microsoft Edge 中运行测试?

python - 使用 Python Selenium webdriver 登录雅虎电子邮件帐户

xml - 用于比较两个元素的两个不同属性的 XPath

Python 3 导入错误 : No module named 'ConfigParser'

python - 如何从列表中的整数中删除重复的数字

java - 单击相关按钮

python - 在 xpath 中使用列表变量