靓汤由div class nam获得价值

2024-05-17 11:34:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我要用Python靓汤来取消第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>

Tags: texthttpsdivhtmlliproductitemprice
1条回答
网友
1楼 · 发布于 2024-05-17 11:34:57

替换: 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

相关问题 更多 >