簡體   English   中英

無法使用beautifulsoup從span-tag內部獲取數據

[英]Can't get data from inside of span-tag with beautifulsoup

我正在嘗試抓取Instagram 頁面,並且想要獲取/訪問存在於 span-tag 中的 div-tags。 但我不能! Instagram 頁面的 HTML 看起來像

 <head>--</head>
    <body>
       <span id="react-root" aria-hidden="false">
       <form enctype="multipart/form-data" method="POST" role="presentation">…</form>
       <section class="_9eogI E3X2T">
          <main class="SCxLW  o64aR" role="main">
             <div class="v9tJq VfzDr">
                 <header class=" HVbuG">…</header>
                 <div class="_4bSq7">…</div>
                 <div class="fx7hk">…</div>
             </div>
          </main>
      </section>
    </body>

我願意,就像

from bs4 import BeautifulSoup
import urllib.request as urllib2
html_page = urllib2.urlopen("https://www.instagram.com/cherrified_/?hl=en")
soup = BeautifulSoup(html_page,"lxml")
span_tag = soup.find('span') # return span-tag correctly
span_tag.find_all('div')    # return empty list, why ?

還請指定一個例子。

Instagram 是一個由 React 提供支持的單頁面應用程序,這意味着它的源只是一個簡單的“空”頁面,它加載 JavaScript 以在下載后在瀏覽器中動態生成內容。

單擊“查看源代碼”或在 Chrome 中訪問view-source:https://www.instagram.com/cherrified_/?hl=en 這是您使用urllib.request下載的 HTML。

您可以看到有一個<span>標記,其中不包含<div>標記。 (注意: <div>內的<span> 是不允許的)。

以這種方式抓取 instagram.com 是不可能的。 它也可能不合法(我不是律師)。

筆記:

  • 您的 HTML 代碼示例不包含<span>的結束標記。
  • 您的 HTML 代碼示例與您在 python 代碼段中提供的鏈接不匹配。
  • 在 python 片段的最后一行中,您可能指的是span_tag.find_all('div') (注意變量名稱和單數'div' )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM