美丽的汤通过div类名称获得价值

时间:2018-12-06 10:58:10

标签: python parsing beautifulsoup

我正在用Python Beautiful Soup抓取https://silpo.ua/offers/cina-tizhnya页。

我想从<li class="normal">接收名称,价格和图像来源

 URL = "https://silpo.ua/offers/sensacijni-znizhki"
'''
driver = webdriver.Firefox()
driver.get(URL)
html = driver.page_source
'''

# html string is a content of one product
html = r'<li class="normal"><a class="product-list__item normal size-normal" href="/offers/cina-tizhnya/grieipfrut"><div class="product-list__item-image" style=""><img alt="" src="https://content.silpo.ua/uploads/2018/12/06/5c08c0fe3508a.png"/></div><div class="product-list__item-content"><div class="product-price product-list__item-price"><div class="product-price__integer">27</div><div class="product-price__other"><div class="product-price__fraction">99</div><div class="product-price__old"><!-- react-text: 287 -->32.99<!-- /react-text --><div class="product-price__old-cut"><i class="icon icon-price-cut"><svg enable-background="new 0 0 46 13" height="13px" version="1.1" viewbox="0 0 46 13" width="46px" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px"><path clip-rule="evenodd" d="M0,11.7L45.6,0L46,1.3L0.4,13L0,11.7z" fill="#A2A2A2" fill-rule="evenodd"></path></svg></i></div></div></div></div><div class="product-list__item-description"><div class="product-list__item-title heading3">Грейпфрут</div><div class="product-list__item-weight">кг</div><hr/></div></div><div class="product-list__item-period"><!-- react-text: 295 -->Пропозиція діє:<!-- /react-text --><br/><span>06.12.2018</span><!-- react-text: 298 --> - <!-- /react-text --><span>12.12.2018</span></div></a></li>'
soup = bs4.BeautifulSoup(html,'html.parser')

resProduct = soup.findAll("li", {"class": "normal"})
type(resProduct) # <class 'bs4.element.ResultSet'>
type(resProduct[0]) # <class 'bs4.element.Tag'>
resProduct[0].img["src"] # It works --> https://content.silpo.ua/uploads/2018/12/06/5c08c0fe3508a.png
res = resProduct[0].div["product-list__item-title heading3"] # ERROR I want "Грейпфрут"
# <div class="product-list__item-title heading3">Грейпфрут</div>

1 个答案:

答案 0 :(得分:1)

替换: resProduct[0].div["product-list__item-title heading3"]

具有: resProduct[0].findAll('div',{"class": "product-list__item-title heading3"})

那应该可以满足您的需求。

然后,如果您要查找该字符串,请使用: resProduct[0].findAll('div',{"class": "product-list__item-title heading3"})[0].text