我可以用“;”分隔所有数据吗?使用XPath?

时间:2018-06-05 08:55:53

标签: python selenium xpath beautifulsoup webdriver

我无法通过xpath打印出来自特定网站的// * [@ class =“footballmaincontent”],谢谢!

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

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码获取所需的输出:

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