【问题标题】:BeautifulSoup finding children with only 'dot', without 'find()' functionBeautifulSoup 找到只有“点”的孩子,没有“find()”功能
【发布时间】:2020-03-28 08:08:56
【问题描述】:

我正在尝试将特定元素初始化为变量,而不使用“find()”或 findAll() 函数,只使用 BeautifulSoup 和“dots”。 我感到困惑的是优先顺序。 使用 findAll() 函数有很多“寻找孩子”的问题,但似乎没有“点”的东西。所以最后我写了这个问题。

这是示例。

-----html------

<div class="item-container">
    <!--product image-->
    <a class="item-img" href="https://www.newegg.com/asus-geforce-rtx-2080-ti-dual-rtx2080ti-o11g/p/N82E16814126247?Item=N82E16814126247">
        <div class="item-badges">
            <div class= "item-test">
             </div>
        </div>
        <img alt="ASUS Dual GeForce RTX 2080 Ti DirectX 12 DUAL-RTX2080TI-O11G 11GB 352-Bit GDDR6 PCI Express 3.0 HDCP Ready SLI Support Video Card" class="lazy-img" data-effect="fadeIn" data-src="//c1.neweggimages.com/NeweggImage/ProductImageCompressAll300/14-126-247-V50.jpg" src="//c1.neweggimages.com/WebResource/Themes/2005/Nest/blank.gif" title="ASUS Dual GeForce RTX 2080 Ti DirectX 12 DUAL-RTX2080TI-O11G 11GB 352-Bit GDDR6 PCI Express 3.0 HDCP Ready SLI Support Video Card">
        </img>
    </a>
    <div class="item-info">
        <!--brand info-->
        <div class="item-branding">
            <a class="item-brand" href="https://www.newegg.com/ASUS/BrandStore/ID-1315">
                <img alt="ASUS" class="lazy-img" data-effect="fadeIn" data-src="//c1.neweggimages.com/Brandimage_70x28//Brand1315.gif" src="//c1.neweggimages.com/WebResource/Themes/2005/Nest/blank.gif" title="ASUS">
                </img></a>
            <!--rating info-->
            <a class="item-rating" href="https://www.newegg.com/asus-geforce-rtx-2080-ti-dual-rtx2080ti-o11g/p/N82E16814126247?Item=N82E16814126247&amp;SortField=0&amp;SummaryType=0&amp;PageSize=10&amp;SelectedRating=-1&amp;VideoOnlyMark=False&amp;IsFeedbackTab=true#scrollFullInfo" title="Rating + 2"><i class="rating rating-2"></i><span class="item-rating-num">(32)</span></a>
        </div>
</div>
</div>

--------蟒蛇--------

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = "https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?Tpk=graphic%20card"
uClient = uReq(my_url)
page_html = uClient.read() 
uClinet.close()
page_soup = soup(page_html, "html.parser")

从这里,我将找到带有“item-branding”类的 div。 所以我就这样了

>>> page_soup.div.div

你从中得到了什么?似乎它取决于python的版本或其他东西。因用户而异。

有人得到,但我得到''。 你看到这个区别了吗?

第一个(which got ),获取第一级子级中的 div。 div class 'item-badges' 是孩子 a(class 'item img') 的孩子,所以它是第二级孩子。因此,拼写“page_soup.div”在第一个“a(item-image)”中跳过了 div(item-branding),并作为第一个“div”被捕获。所以'page_soup.div.div'可以直接进入'div class="item-branding"'。

但是,第二个,我的电脑用同样的咒语做同样的事情。

我的拼写 'page_soup.div.div' 找到了 'div class="item-test"'。 我的咒语'page_soup.div'进入了离顶部最近的div,没有考虑孩子的水平。它刚刚进入第一个 div,即在子 'a(class item-badges)' 内。所以 'page_soup.div.div' 进入了 'div class="item-test"',item-badges 中的第一个 div。

相同的咒语,但不同的逻辑。

您知道是什么造成了这种差异吗?以及如何解决这个问题?

谢谢你的天才。

ps 我使用 python 3.7 32x

【问题讨论】:

    标签: python python-3.x beautifulsoup


    【解决方案1】:

    您的问题已在此处详细记录:BS: navigating using tag names

    浏览解析树的最简单方法是说出您想要的标签的名称。如果你想要&lt;head&gt; 标签,就说soup.head

    您可以一次又一次地使用这个技巧来放大解析树的某个部分。 soup.body.b 获取&lt;body&gt; 标记下方的first &lt;b&gt; 标记。

    使用标签名称作为属性只会为您提供该名称的第一个标签。

    如果您需要获取所有&lt;a&gt; 标记,或者比具有特定名称的第一个标记 更复杂的任何标记,则需要使用Searching the tree 中描述的方法之一,如find_all()

    (强调和省略我的)

    所以你的page_soup.div.div 找到了div 中的第一个div - 而page_soup.div 找到了第一个 div。 p>

    <html>
    
    <head>
      <title>The Dormouse's story</title>
    </head>
    
    <body>
      <div>first div</div>
      <p>unrelated
      </p>
      <div>second div
        <div>with another div inside</div>
      </div>
    
      <div>can't get this one by soup.div.div
        <div>with another div inside</div>
      </div>
    </body

    对于该代码,您可以通过soup.div 获得第一个代码,通过.div.div 获得第二个代码。最后一个你只能通过findall()获得。

    【讨论】:

      猜你喜欢
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      相关资源
      最近更新 更多