我正在尝试使用 Selenium 将表中的多个列解析为字典,但我的速度似乎很慢。我正在使用 python、Selenium 2.0 和 webdriver.Chrome()
table = self.driver.find_element_by_id("thetable")
# now get all the TR elements from the table
all_rows = table.find_elements_by_tag_name("tr")
# and iterate over them, getting the cells
for row in all_rows:
cells = row.find_elements_by_tag_name("td")
# slowwwwwwwwwwwwww
dict_value = {'0th': cells[0].text,
'1st': cells[1].text,
'2nd': cells[2].text,
'3rd': cells[3].text,
'6th': cells[6].text,
'7th': cells[7].text,
'10th': cells[10].text}
问题似乎是获取每个 td 元素的“文本”属性。有没有更快的方法?
最佳答案
备选方案。
如果稍后(在循环之后),您不需要 selenium 为您提供的交互性 - 您可以传递当前的 HTML source code页面的 lxml.html
,以速度着称。示例:
import lxml.html
root = lxml.html.fromstring(driver.page_source)
for row in root.xpath('.//table[@id="thetable"]//tr'):
cells = row.xpath('.//td/text()')
dict_value = {'0th': cells[0],
'1st': cells[1],
'2nd': cells[2],
'3rd': cells[3],
'6th': cells[6],
'7th': cells[7],
'10th': cells[10]}
关于python - 在 Selenium 中快速获取表格的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27592444/