Python helium 点击后获取 table 的内容

Python helium get contents of table after click

我正在使用氦气抓取网页。

result = start_firefox(                                                                                   
    "https://www.medtronic.com/covidien/en-us/products/brain-monitoring/bis-monitoring-system.html",      
)                                                                                                         
                                                                                                          
result.find_element_by_class_name("js-open-table-overlay").click()   

点击操作后,我看到一个 table,我需要抓取 table 的内容,但是我如何 select table点击 ?

您需要使用 find_elements_... 来获取所有 <table>,并使用 for-loop 分别处理每个 table 并使用(嵌套)for-循环得到<tr>(行)和<th>(header)在table,并使用(嵌套)for-循环得到<td>(单元格)在行中。并且您必须添加元素以更正(嵌套)列表。


但是这个页面使用的是普通的<table>,而这个table一直在HTML中(不是javaScript添加的)所以可以更简单的使用pandas.read_html() 得到所有 <table>

import pandas as pd

url = "https://www.medtronic.com/covidien/en-us/products/brain-monitoring/bis-monitoring-system.html"

all_tables = pd.read_html(url)

for table in all_tables:
    print(table.to_string())

结果

     ORDER CODE                                                      DESCRIPTION UNIT OF MEASURE  QUANTITY
0      186-1014                        BIS™ Complete 4-Channel Monitoring System            Each         1
1      186-0210                                  BIS™ Complete 2-Channel Monitor            Each         1
2  186-0224-AMS  BIS™ LOC 4-Channel Monitor with Patient Interface Cable (PIC-4)            Each         1
3  186-0195-AMS          BIS™ LOC 2-Channel Monitor with Patient Interface Cable            Each         1
4      186-0212                                            BIS™ Bilateral Sensor            Each         1

              DISPLAY                                               BIS™ COMPLETE 2-CHANNEL MONITOR                                     BIS™ COMPLETE 4-CHANNEL MONITORING SYSTEM
0          Parameters                                                    BIS, SQI, EMG, SR, BC, EEG                                           BIS, SQI, EMG, SR, BC, TP, SEF, EEG
1  Trended parameters                                                         BIS, SQI, EMG, SR, BC                                                    BIS, SQI, EMG, SR, BC, SEF
2          BIS™ alarm  Upper and lower limit, results in visual and audible alert when out of range  Upper and lower limit, results in visual and audible alert when out of range