Pea*_*rel 6 python selenium html-table webdriver selenium-webdriver
我正在尝试使用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}
Run Code Online (Sandbox Code Playgroud)
问题似乎是获得每个td元素的'text'属性.有更快的方法吗?
替代选择.
如果稍后(在循环之后),您不需要selenium为您提供的交互性 - 您可以将页面的当前HTML源代码传递给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]}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10019 次 |
| 最近记录: |