【问题标题】:how do I get all text separated in nested div under 1 element ID如何在 1 个元素 ID 下的嵌套 div 中分隔所有文本
【发布时间】:2017-03-03 05:46:09
【问题描述】:

父链接下的监控选项卡: http://www.pajak.go.id/statistik-amnesti

使用 Python,我正在尝试提取 https://monitoringamnesti.pajak.go.id/viewer/dashboard?dashboardguid=90a16bf8-d418-4ed4-8160-7f883f601dd0&v=636126392121123334&style=Default 的左上角表格

我的代码:

import selenium.webdriver as driver

browser = driver.Chrome()  

url=  "https://monitoringamnesti.pajak.go.id/viewer/public/dashboard?name=Monitoring_Amnesti_Pajak"

browser.get(url)

all_text = browser.execute_script("returndocument.getElementById('SimpleDataGrid-viewport').textContent")

但是,所有文本都集中在一起。有没有办法可以将表中的所有信息作为列表/数据框获取?

HTML 代码:

<div id="SimpleDataGrid-viewport" class="datagrid-viewport" style="width: 120px; height: 376px;">
<div id="SimpleDataGrid-spacer-clip" class="datagrid-spacer-clip clip _hidden" style="width: 22px; height: 23px;">
    <div id="SimpleDataGrid-spacer" class="datagrid-spacer" style="width: 22px; height: 23px;">
        <div class="row">
            <div class="cell blank" style="border-bottom-color: rgb(0, 153, 195); width: 22px; height: 11px;">&nbsp;
            </div>
        </div>
    </div>
</div>
<div id="SimpleDataGrid-head-clip" class="datagrid-head-clip clip" style="width: 120px; margin-left: 0px; height: 23px;">
<div id="SimpleDataGrid-head" class="datagrid-head" style="top: 0px; left: 0px; width: 552px;">
    <div class="row">
        <div class="cell column0 text sortable" data-type="text" data-index="0" data-sortorder="unsorted" style="border-bottom-color: rgb(0, 153, 195); width: 161px; height: 11px;">Jenis<em class="unsorted" data-sortorder="unsorted"></em>
        </div>
        <div class="cell column1 number sortable" data-type="number" data-index="1" data-sortorder="unsorted" style="border-bottom-color: rgb(0, 153, 195); width: 52px; height: 11px;">Juli<em class="unsorted" data-sortorder="unsorted"></em>
        </div>
        <div class="cell column2 number sortable" data-type="number" data-index="2" data-sortorder="unsorted" style="border-bottom-color: rgb(0, 153, 195); width: 58px; height: 11px;">Agustus<em class="unsorted" data-sortorder="unsorted"></em>
        </div>
        <div class="cell column3 number sortable" data-type="number" data-index="3" data-sortorder="unsorted" style="border-bottom-color: rgb(0, 153, 195); width: 73px; height: 11px;">September<em class="unsorted" data-sortorder="unsorted"></em></div>
        <div class="cell column4 number sortable" data-type="number" data-index="4" data-sortorder="unsorted" style="border-bottom-color: rgb(0, 153, 195); width: 58px; height: 11px;">Oktober<em class="unsorted" data-sortorder="unsorted"></em>
        </div>

【问题讨论】:

  • 能贴出相关的HTML代码吗?您在问题中提到的链接要求提供登录凭据。
  • 嗨,我已经在里面添加了主链接。复制这里的html代码似乎太长太乱了。

标签: html python-2.7 selenium


【解决方案1】:

正如 Sudharsan 所述,您提供的 URL 要求登录凭据,因此我们看不到您引用的内容。

如果您想使用 Selenium 执行此操作,那么您需要指示 Selenium 拉取感兴趣的表,然后将其输出为您感兴趣的任何形式,而不是简单地要求 selenium 执行 Javascript。

根据您提供的代码,我猜您可以通过查看此代码来解决您的需求,其中 css 选择器在网格的每一行中查找所有“列”:

# print the content of each row in the table (this will include the headers)
for cell in browser.find_elements_by_css_selector("div[id='SimpleDataGrid-head'] div.row div.cell"):
    print(cell.text)

【讨论】:

  • 如果您的“table”不是带有“tr”行的“table”元素,那么如果您可以向我们提供显示前几行的输出,我们可能会帮助您想出一个标识行的路径。
  • 感谢您的帮助。但是,表格不是表格元素。我在那里添加了部分html。
  • 我已经使用 css 选择器编辑了上面的答案以查找所有单元格。您可以将其分成两个循环,其中第一个循环使用选择器“div[id='SimpleDataGrid-head'] div.row”查找“行”,然后对于每一行,使用 row.find_elements_by_css_selector( “div.cell”)
  • 酷@j.j。很高兴它对你有用。你介意接受答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
相关资源
最近更新 更多