【问题标题】:How to scrape invisible part of HTML code如何抓取 HTML 代码的不可见部分
【发布时间】:2020-02-21 16:43:35
【问题描述】:

我需要抓取网站https://e-mapa.net/ 那是波兰的灾难记录。准确地说,我想从 id 'gfi_0' 标记的元素中获取数据。问题是这个元素不能立即访问,只有在点击地图上的任何地方后才能访问。早些时候,它甚至不存在于 html 代码中(这部分显示在点击时)。您可以在下面找到这部分 html 代码。

<div style="position: absolute; left: 270px; top: 170px; width: 330px; height: 350px; background-image: url(&quot;application/system/pandora/images/window_back.jpg&quot;); border: 1px solid rgb(68, 68, 85); background-position: 0px 0px; background-repeat: repeat; border-radius: 5px; z-index: 100051; display: block;">
<div style="position: absolute; left: 6px; top: 0px; right: 24px; height: 22px; padding-top: 4px; cursor: move; overflow: hidden; font-family: tahoma; font-size: 14px; font-weight: bold; color: rgb(255, 255, 255);">Informacja&nbsp;o&nbsp;obiekcie</div>
<div style="position: absolute; inset: 26px 6px 6px; background-color: rgb(241, 244, 248); overflow: auto; border: 1px solid rgb(102, 102, 102); padding: 2px 5px 5px;">
<div style="position: relative;">
<div style="position: relative; height: 25px; background-color: rgb(204, 204, 204); margin: 6px 0px 4px; cursor: pointer;">
<div style="position: absolute; inset: 0px 24px 0px 0px;"></div>
<div style="position: absolute; top: 4px; left: 24px; font-size: 14px; font-weight: bold; color: rgb(51, 51, 51);">Ewidencja&nbsp;gruntów&nbsp;i&nbsp;budynków</div>
<div style="position: absolute; left: 7px; top: 2px; width: 16px; height: 16px; font-size: 16px; font-weight: bold; color: rgb(51, 51, 51); display: block;">+</div>
<div style="position: absolute; left: 7px; top: 2px; width: 16px; height: 16px; font-size: 16px; font-weight: bold; color: rgb(51, 51, 51); display: none;">-</div>
<div style="position: absolute; top: 4px; right: 5px; width: 16px; height: 16px; background-image: url(&quot;application/images/print.png&quot;); background-repeat: no-repeat; background-position: center center;"></div>
</div>
<div style="position: relative; display: none;" id="gfi_0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>101102_2.0005.unknown</title>
<table style="color: #000; font-size: 11px;">
    <tbody>
<tr><td>Identyfikator działki</td><td>101102_2.0005.unknown</td></tr>
<tr><td>Województwo</td><td>Łódzkie</td></tr>
<tr><td>Powiat</td><td>Poddębicki</td></tr>
<tr><td>Gmina</td><td>Pęczniew</td></tr>
<tr><td>Obręb</td><td>Drużbin</td></tr>
<tr><td>Numer działki</td><td>unknown</td></tr>
<tr><td>KW</td><td>brak informacji</td></tr>
<tr><td>Data publikacji danych</td><td>2019-10-25</td></tr>
<tr><td>Informacje dodatkowe o działce</td><td>Organem odpowiedzialnym za dane ewidencji gruntów i budynków jest Starosta Powiatu (ustawa Prawo geodezyjne i kartograficzne art. 7d pkt 1, Dz.U. z 2019 r. poz. 725).</td></tr>
</tbody>
</table>
</div>
</div
</div>
<div class="closeWindowButton" style="top: 4px; right: 2px;"></div>
<div class="collapseWindowButton" style="right: 19px;"></div>
</div>

我试过了

driver.get('https://e-mapa.net/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "gfi_0")))

或

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "gfi_0")))

但我收到错误,无法找到该元素

请帮忙

【问题讨论】:

    标签: python html selenium web-scraping html-table


    【解决方案1】:

    恐怕这不是html的不可见部分,而只是不存在的部分,除非您在地图上单击所需的点。当您单击它时,内部 javascript 会检测您单击的位置并生成一个单独的 http 请求到 https://e-mapa.net/application/system/get_feature.php,并传递您单击的参数。响应如下所示:

    <body>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>301505_5.0001.846/1</title>
        </head>
        <table style="color: #000; font-size: 11px;">
            <tr>
                <td>Identyfikator działki</td>
                <td>301505_5.0001.846/1</td>
            </tr>
            <tr>
                <td>Województwo</td>
                <td>Wielkopolskie</td>
            </tr>
            <tr>
                <td>Powiat</td>
                <td>Nowotomyski</td>
            </tr>
            <tr>
                <td>Gmina</td>
                <td>Gmina Opalenica</td>
            </tr>
            <tr>
                <td>Obręb</td>
                <td>DAKOWY MOKRE</td>
            </tr>
            <tr>
                <td>Numer działki</td>
                <td>846/1</td>
            </tr>
            <tr>
                <td>KW</td>
                <td>brak informacji</td>
            </tr>
            <tr>
                <td>Data publikacji danych</td>
                <td>2019-10-24</td>
            </tr>
            <tr>
                <td>Informacje dodatkowe o działce</td>
                <td>Organem odpowiedzialnym za dane ewidencji gruntów i budynków jest Starosta Powiatu (ustawa Prawo
                    geodezyjne i kartograficzne art. 7d pkt 1, Dz.U. z 2019 r. poz. 725).
                </td>
            </tr>
        </table>
    </body>
    

    它被解析并用作点击前不存在的新DOM节点的数据源。

    因此,简而言之,您必须单击测试中的某个位置,然后才能访问所需的数据。

    【讨论】:

      猜你喜欢
      • 2018-09-16
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多