【问题标题】:how to find div in beautifulsoup if class or id doesn't exist如果 class 或 id 不存在,如何在 beautifulsoup 中找到 div
【发布时间】:2020-08-03 22:50:42
【问题描述】:
<div style="width: 860px; background: #fafafa; padding: 10px 20px; color:#444; font-size: 10pt; border-bottom: 1px solid #eee; line-height: 40px;">                                           
                    <div style="float: left; width: 50px; color:#888;">1st</div>
            <div style="float: left; width: 70px; font-size: 1.1em; font-weight: bold;">
                <span style="color:#00bee7;">A++</span>            </div>                     
            <div style="float: left; width: 350px; line-height: 25px;">
                                   <img src="https://scontent-mia3-2.xx.fbcdn.net/v/t51.2885-15/59381178_2348911458724961_5863612957363011584_n.jpg?_nc_cat=1&amp;_nc_sid=86c713&amp;_nc_eui2=AeGtKP5VzxWyws1yzQYTvoBRGYIx5tPrnUEZgjHm0-udQbHsS9qt_TKk8q5kIkdkESQ&amp;_nc_ohc=-6BDpTfLubYAX-JiWzZ&amp;_nc_ht=scontent-mia3-2.xx&amp;oh=66f054be5f55ac3442a6e3f6ec42c8b5&amp;oe=5E" style="height: 40px; width: 40px; vertical-align: middle; margin-right: 10px;">  

                   <a href="/instagram/user/instagram">Instagram</a>

            </div>

            <div style="float: left; width: 80px;"><span style="color:#555;">6,310</span></div>
            <div style="float: left; width: 150px;">
                <span style="color:#555;">344,577,749</span>            </div>
            <div style="float: left; width: 150px;">
                <span style="color:#555;">223</span>            </div>
        <div style="clear: both;"></div></div>

无法通过美丽的汤找到这个 div。 你有解决这个问题的办法吗?

【问题讨论】:

标签: web-scraping beautifulsoup python-beautifultable


【解决方案1】:

你可以尝试获取这个div元素:

import requests
from bs4 import BeautifulSoup as bs

header =  {'User-Agent': 'Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0 '}
url = "https://socialblade.com/instagram/top/100/followers"
html_doc = requests.get(url,headers=header)

soup = bs(html_doc.text,'lxml')

div = soup.find('div', style="width: 860px; background: #fafafa; padding: 10px 20px; color:#444; font-size: 10pt; border-bottom: 1px solid #eee; line-height: 40px;")

print(div)

输出将是:

<div style="width: 860px; background: #fafafa; padding: 10px 20px; color:#444; font-size: 10pt; border-bottom: 1px solid #eee; line-height: 40px;">
<div style="float: left; width: 50px; color:#888;">1st</div>
<div style="float: left; width: 70px; font-size: 1.1em; font-weight: bold;">
<span style="color:#00bee7;">A++</span> </div>
<div style="float: left; width: 350px; line-height: 25px;">
<img src="https://scontent-mia3-2.xx.fbcdn.net/v/t51.2885-15/59381178_2348911458724961_5863612957363011584_n.jpg?_nc_cat=1&amp;_nc_sid=86c713&amp;_nc_eui2=AeEh5pbny_ctACFHCrNfGMRvGYIx5tPrnUEZgjHm0-udQTaWM4iakXddnQd0R4c-d4Q&amp;_nc_ohc=pKzPi-1yHUYAX9PJuAw&amp;_nc_ht=scontent-mia3-2.xx&amp;oh=68ea9b1c2f1c4a8b3e9b87c3a02178a6&amp;oe=5E" style="height: 40px; width: 40px; vertical-align: middle; margin-right: 10px;"/>
<a href="/instagram/user/instagram">Instagram</a>
</div>
<div style="float: left; width: 80px;"><span style="color:#555;">6,312</span></div>
<div style="float: left; width: 150px;">
<span style="color:#555;">344,721,732</span> </div>
<div style="float: left; width: 150px;">
<span style="color:#555;">225</span> </div>
<div style="clear: both;"></div></div>

【讨论】:

  • 这样做我得到了一个空数组
  • 现在我检查了一下,我得到了上面的结果。请仔细检查,我想你得到了上面的div 元素。
  • 我没有传递HTML代码,而是传递了页面的链接,我认为这应该和你上面提到的一样。我说的对吗??
  • 现在更新我的代码。如果您通过了链接,您将获得与您提到的相同的输出。只有一个 div 和你提到的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多