【问题标题】:BeautifulSoup parsing issues some div not showingBeautifulSoup 解析问题一些 div 没有显示
【发布时间】:2020-11-09 16:38:04
【问题描述】:

我正在尝试解析此页面:https://www.ldlc.com/fr-be/informatique/pieces-informatique/carte-professionnelle/c4685/

问题是,在这个元素中:https://gyazo.com/e544be64a41a121bdb0c0f71aef50692, 我想要包含价格的 div。如果你检查页面,你可以看到这部分的 html 代码,如下所示:

<div class="price">
  <div class"price">
    "thePrice"
    <sup>93</sup>
  </div>
</div>

但是,当使用 page_soup = soup(my_html_page, 'html.parser')page_soup = soup(my_html_page, 'lxml')page_soup = soup(my_html_page, 'html5lib') 时,我只得到该部分的结果:

<div class="price"></div>

就是这样。我一直在互联网上搜索几个小时,以弄清楚为什么内部 div 没有被解析。

三个不同的解析器,如果这是问题的话,似乎没有一个能够通过内部子级与其父级共享相同类名这一事实。

【问题讨论】:

    标签: python html parsing beautifulsoup


    【解决方案1】:

    希望对你有所帮助。

    from bs4 import BeautifulSoup
    import requests
    
    url = 'https://www.ldlc.com/fr-be/informatique/pieces-informatique/carte-professionnelle/c4685/'
    html = BeautifulSoup(requests.get(url).content, 'html.parser')
    prices = html.find_all("div", {"class": "price"})
    for price in prices:
        print(price.text)
    

    打印输出

    561€95 
     169€94 
     165€95 
     1 165€94 
     7 599€95 
     267€95 
     259€94 
     599€95 
     511€94 
     1 042€94 
     2 572€94 
     783€95 
     2 479€94 
     2 699€95 
     499€94 
     386€95 
     169€94 
     2 343€95 
     783€95 
     499€94 
     499€94 
     259€94 
     267€95 
     165€95 
     169€94 
     2 399€95 
     561€95 
     2 699€95 
     2 699€95 
     6 059€95 
     7 589€95 
     10 991€95 
     9 619€94 
     2 479€94 
     3 135€95 
     7 589€95 
     511€94 
     1 042€94 
     386€95 
     599€95 
     1 165€94 
     2 572€94 
     783€95 
     2 479€94 
     2 699€95 
     499€94 
     169€94 
     2 343€95 
     2 699€95 
     3 135€95 
     6 816€95 
     7 589€95 
     561€95 
     267€95 
    

    【讨论】:

      【解决方案2】:

      要抓取class="price"&gt; 的所有价格,请参阅此示例:

      import requests
      from bs4 import BeautifulSoup
      
      
      url = 'https://www.ldlc.com/fr-be/informatique/pieces-informatique/carte-professionnelle/c4685/'
      soup = BeautifulSoup(requests.get(url).content, 'html.parser')
      # Select all the 'price' classes
      for tag in soup.select('div.price'):
          print(tag.text)
      

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 2010-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 2015-02-24
        • 1970-01-01
        • 2014-12-10
        相关资源
        最近更新 更多