Mary
Mary

Reputation: 797

Can I separate all data by ";" using XPath?

I can't print out //*[@class="footballmaincontent"] from the specific website by xpath, thanks!

# -*- coding:UTF-8 -*-

from pyvirtualdisplay import Display
import sys
from bs4 import BeautifulSoup
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

driver = webdriver.Firefox()
driver.get("url")

soup = BeautifulSoup(driver.page_source.encode('utf-8'),'html.parser')
lines = soup.find_elements_by_xpath('//*[@class="footballmaincontent"]/tr')
print lines

driver.close()
display.stop()

Upvotes: 0

Views: 85

Answers (1)

Andersson
Andersson

Reputation: 52665

Try below code to get required output:

lines = ['; '.join([j.text for j in i.find_elements_by_xpath('./td/div | ./td/span/span | ./td/span[not(span)]') if j.text]) for i in driver.find_elements_by_xpath('//*[@class="footballmaincontent"]//tr[position()>2]') if i.text]

for line in lines:
    print line

Upvotes: 1

Related Questions