selenium + python 获取table数据的示例讲解
作者:hh-glimmer 时间:2021-02-20 11:57:46
方法一:
<code class="language-python">"""
根据table的id属性和table中的某一个元素定位其在table中的位置
table包括表头,位置坐标都是从1开始算
tableId:table的id属性
queryContent:需要确定位置的内容
"""
def get_table_content(tableId,queryContent):
arr = []
arr1 = []
table_loc = (By.ID,tableId)
# 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据
table_tr_list = driver.find_element(*table_loc).find_elements(By.TAG_NAME, "tr")
for tr in table_tr_list:
arr1 = (tr.text).split(" ") #以空格拆分成若干个(个数与列的个数相同)一维列表
# print(tr.text)
# print(arr1)
arr.append(arr1) #将表格数据组成二维的列表
#循环遍历table数据,确定查询数据的位置
for i in range(len(arr)):
for j in range(len(arr[i])):
if queryContent== arr[i][j]:
print("%r坐标为(%r,%r)" %(queryContent,i+1,j+1))
get_table_content("myTable","第二行第二列")</code>
方法二:
"""
根据table的id属性和table中的某一个元素定位其在table中的位置
table包括表头,位置坐标都是从1开始算
tableId:table的id属性
queryContent:需要确定位置的内容
"""
def get_table_content(tableId,queryContent):
# 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据
table_tr_list = driver.find_element(By.ID, tableId).find_elements(By.TAG_NAME, "tr")
table_list = [] #存放table数据
for tr in table_tr_list: #遍历每一个tr
#将每一个tr的数据根据td查询出来,返回结果为list对象
table_td_list = tr.find_elements(By.TAG_NAME, "td")
row_list = []
print(table_td_list)
for td in table_td_list: #遍历每一个td
row_list.append(td.text) #取出表格的数据,并放入行列表里
table_list.append(row_list)
# 循环遍历table数据,确定查询数据的位置
for i in range(len(table_list)):
for j in range(len(table_list[i])):
if queryContent== table_list[i][j]:
print("%r坐标为(%r,%r)" %(queryContent,i+1,j+1))
get_table_content("myTable","第二行第二列")
方法三:
"""
根据CSS选择器和table中的某一个元素定位其在table中的位置
table包括表头,位置坐标都是从1开始算
cssSelector:table的CSS选择器属性
queryContent:需要确定位置的内容
"""
def get_table_content(cssSelector,queryContent):
locator = cssSelector + ">tbody>tr"
table_list = []
table_tr_list = driver.find_elements(By.CSS_SELECTOR, locator)[1:] #去掉表头
for tr in table_tr_list:
row_list = []
table_td_list = tr.find_elements(By.TAG_NAME,"td")
for td in table_td_list:
row_list.append(td.text)
table_list.append(row_list)
for i in range(len(table_list)):
for j in range(len(table_list[i])):
if queryContent== table_list[i][j]:
print("%r坐标为(%r,%r)" %(queryContent,i+1,j+1))
get_table_content("#myTable","第二行第二列")
来源:https://blog.csdn.net/huayuhuan/article/details/76559465
标签:selenium,python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解pandas绘制矩阵散点图(scatter_matrix)的方法
2022-06-09 18:52:34
![](https://img.aspxhome.com/file/2023/7/107207_0s.png)
纯ASP(VBscript)写的全球IP地址搜索程序
2007-09-27 13:28:00
Python实现的金山快盘的签到程序
2022-12-05 21:14:51
Oracle数据库SQL语句性能调整的基本原则
2009-03-25 16:55:00
Django nginx配置实现过程详解
2022-11-09 11:42:58
一些文档中没有介绍的SQL Server DBCC命令
2008-12-09 14:24:00
漂亮的Django Markdown富文本app插件的实现
2021-08-14 15:23:24
![](https://img.aspxhome.com/file/2023/3/80533_0s.jpg)
Python 基于xml.etree.ElementTree实现XML对比示例详解
2022-02-24 12:25:53
python实现网络五子棋
2021-10-16 23:58:17
![](https://img.aspxhome.com/file/2023/0/93640_0s.jpg)
python实现简易版学生成绩管理系统
2022-01-19 10:52:03
![](https://img.aspxhome.com/file/2023/1/81671_0s.jpg)
python之信息加密题目详解
2023-06-26 09:24:35
python实现电脑自动关机
2021-05-03 13:41:43
python基础之for循环
2021-08-06 02:12:51
![](https://img.aspxhome.com/file/2023/3/81573_0s.png)
python爬虫开发之Request模块从安装到详细使用方法与实例全解
2023-10-30 18:08:50
详解用python生成随机数的几种方法
2022-01-24 14:17:42
kali最新国内更新源sources
2022-03-25 02:53:37
![](https://img.aspxhome.com/file/2023/7/99367_0s.png)
django2+uwsgi+nginx上线部署到服务器Ubuntu16.04
2022-01-15 17:03:09
![](https://img.aspxhome.com/file/2023/8/97678_0s.png)
Python实现随机划分图片数据集的示例代码
2021-08-01 16:35:32
python字符串运算符详情
2023-11-19 20:51:31
![](https://img.aspxhome.com/file/2023/9/64609_0s.png)
详解Python静态网页爬取获取高清壁纸
2023-10-18 08:27:42
![](https://img.aspxhome.com/file/2023/4/63214_0s.png)