【问题标题】:Getting only numbers from BeautifulSoup instead of whole div仅从 BeautifulSoup 获取数字而不是整个 div
【发布时间】:2020-11-24 06:06:26
【问题描述】:

我正在尝试通过创建一个小型 websraping 程序来学习 python,以使生活更轻松,尽管我在使用 BS4 时遇到了仅获取数字的问题。我在抓取实际广告时能够获得价格,但我想从页面上获得所有价格。

这是我的代码:

from bs4 import BeautifulSoup
import requests
prices = []
url = 'https://www.kijiji.ca/b-cars-trucks/calgary/new__used/c174l1700199a49?ll=51.044733%2C-114.071883&address=Calgary%2C+AB&radius=50.0'
result = requests.get(url)
print (result.status_code)
src = result.content
soup = BeautifulSoup(src, 'html.parser')
print ("CLEARING")
price = soup.findAll("div", class_="price")
prices.append(price)
print (prices)

这是我的输出

[<div class="price">
                        
                            
                            
                                
                                
                                    $46,999.00
                                    
                                    
                                    
                                
                                
                            
                            

                            
                                
                                    <div class="dealer-logo">
<div class="dealer-logo-image">
<img src="https://i.ebayimg.com/00/s/NjBYMTIw/z/xMQAAOSwi9ZfoW7r/$_69.PNG"/>
</div>
</div>
</div>

理想情况下,我只希望输出为“46,999.00”。

我尝试使用 text=True,虽然这不起作用,除了一个空列表之外,我不会从中获得任何输出。

谢谢

【问题讨论】:

  • 请通过这个link

标签: python beautifulsoup


【解决方案1】:

您需要获取标签的文本部分,然后对其进行一些正则表达式处理。

import re

def get_price_from_div(div_item):
    str_price = re.sub('[^0-9\.]','', div_item.text)
    float_price = float(str_price)
    return float_price

找到 div 后在代码中调用此方法

from bs4 import BeautifulSoup
import requests
prices = []
url = 'https://www.kijiji.ca/b-cars-trucks/calgary/new__used/c174l1700199a49?ll=51.044733%2C-114.071883&address=Calgary%2C+AB&radius=50.0'
result = requests.get(url)
print (result.status_code)
src = result.content
soup = BeautifulSoup(src, 'html.parser')
print ("CLEARING")
price = soup.findAll("div", class_="price")
prices.extend([get_price_from_div(curr_div) for curr_div in price])
print (prices)

【讨论】:

    【解决方案2】:

    不使用 RegEx 的一个选项是过滤掉 startwith() 美元符号 $ 的标签:

    import requests
    from bs4 import BeautifulSoup
    
    URL = 'https://www.kijiji.ca/b-cars-trucks/calgary/new__used/c174l1700199a49?ll=51.044733%2C-114.071883&address=Calgary%2C+AB&radius=50.0'
    
    soup = BeautifulSoup(requests.get(URL).content, "html.parser")
    
    price_tags = soup.find_all("div", class_="price")
    
    prices = [
        tag.get_text(strip=True)[1:] for tag in price_tags
        if tag.get_text(strip=True).startswith('$')
    ]
    
    print(prices)
    

    输出:

    ['48,888.00', '21,999.00', '44,488.00', '5,500.00', '33,000.00', '14,900.00', '1,750.00', '35,600.00', '1,800.00', '25,888.00', '36,888.00', '32,888.00', '30,888.00', '18,888.00', '21,888.00', '29,888.00', '22,888.00', '30,888.00', '17,888.00', '17,888.00', '16,888.00', '22,888.00', '22,888.00', '34,888.00', '31,888.00', '32,888.00', '30,888.00', '21,888.00', '15,888.00', '21,888.00', '28,888.00', '19,888.00', '18,888.00', '30,995.00', '30,995.00', '30,995.00', '19,888.00', '47,995.00', '21,888.00', '46,995.00', '32,888.00', '29,888.00', '26,888.00', '21,888.00']
    

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多