Python Beautifulsoup-问题阅读<span>

时间:2018-10-27 02:07:33

标签: python web-scraping beautifulsoup

我尝试从以下HTML中提取“品牌徽标”,“产品名称”,“价格”和“最佳价格”:

<div class="container">
  <div class="catalog-wrapper">
    <div class="slideout-filters"></div>
    <section class="catalog-top-banner"></section>
    <section class="search-results">
    <section class="catalog">
      <div class="row">
      <div class="col-xs-12 col-md-4 col-lg-3">
      <div class="col-xs-12 col-md-8 col-lg-9">
        <div class="catalog-container">
          <a class="catalog-product catalog-item ">
            <div class="product-image "></div>
            <div class="product-description">
              <div>
                <div class="brand-logo">
                  <span>PACO RABANNE</span>
                </div>
                <span class="product-name">
                    PACO RABANNE PERFUME MUJER 30 ML
                </span>
                <span class="price">Normal: S/ 219</span>
                <span class="best-price  ">Internet: S/ 209</span>

“品牌徽标”和“产品名称”已完成,但我看不到“价格”和“最佳价格” 我是这样尝试的:

box_3 = soup.find('div','col-xs-12 col-md-8 col-lg-9')   
    for div in box_3.find_all('div','product-description'):
    d={}
    d["Marca"] = div.find_all("div",{"class","brand-logo"})[0].getText()    
    d["Producto"] = div.find_all("span",{"class","product-name"})[0].getText()    
    d["Precio"] = div.find_all('span',class_='price')    
    d["Oferta"] = div.find_all('span',class_='best-price  ')    
    l.append(d)
    l

退出:

{'Marca': 'PACO RABANNE',
'Oferta': [],
 'Precio': [<span class="price">Normal: S/ 219</span>],
 'Producto': 'PACO RABANNE PERFUME MUJER 30 ML'}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

您可以找到"product-description" div,然后遍历所需的div类:

from bs4 import BeautifulSoup as soup
import re
_to_find = ['brand-logo', 'product-name', 'price', 'best-price']
s = soup(content, 'html.parser').find('div', {'class':'product-description'})
final_results = [(lambda x: s.find('span', {'class':i}).text if not x else x.text)(s.find('div', {'class':i})) \
    for i in _to_find] 
filtered = [re.sub('^[\n\s]+|[\n\s]+$', '', i) for i in final_results]

输出:

['PACO RABANNE', 'PACO RABANNE PERFUME MUJER 30 ML', 'Normal: S/ 219', 'Internet: S/ 209']

答案 1 :(得分:1)

不幸的是,没有实际的网站,我无法检查解决方案:(。

也许您应该以与工作状态相同的方式从“不工作”部分中提取数据(这是幸运的猜测-没有网站或只是由bs4解析的网站,我真的无法对其进行测试)。 / p>

d["Precio"] = div.find_all('span',{"class","price"})[0].getText()    
d["Oferta"] = div.find_all('span',{"class","best-price"})[0].getText() 

最好创建新的方法/函数,以获取所选的属性并处理潜在的错误。