Python Beautiful Soup通过div类名获取值
我正在用巨蟒靓汤撕碎书页 我想从Python Beautiful Soup通过div类名获取值,python,parsing,beautifulsoup,Python,Parsing,Beautifulsoup,我正在用巨蟒靓汤撕碎书页 我想从 URL=”https://silpo.ua/offers/sensacijni-znizhki" ''' driver=webdriver.Firefox() 获取驱动程序(URL) html=driver.page\u源 ''' #html字符串是一个产品的内容 html=r'' soup=bs4.BeautifulSoup(html,'html.parser') resProduct=soup.findAll(“li”,“class”:“normal”})
URL=”https://silpo.ua/offers/sensacijni-znizhki"
'''
driver=webdriver.Firefox()
获取驱动程序(URL)
html=driver.page\u源
'''
#html字符串是一个产品的内容
html=r''
soup=bs4.BeautifulSoup(html,'html.parser')
resProduct=soup.findAll(“li”,“class”:“normal”})
类型(反应产物)#
类型(resProduct[0])#
resProduct[0].img[“src”]#它可以-->https://content.silpo.ua/uploads/2018/12/06/5c08c0fe3508a.png
res=resProduct[0].div[“产品列表项目标题3”]#错误我想要“Гццццц”
# Грейпфрут
更换:
resProduct[0]。div[“产品列表\项目标题3”]
与:
resProduct[0]。findAll('div',{“class”:“product-list\uu item-title heading3”})
这应该是你想要的
如果要查找该字符串,请使用:
resProduct[0]。findAll('div',{“class”:“product-list\uu item-title heading3”})[0]。text
因为我正在使用seleniumthis不应该标记为“selenium”@CoreyGoldberg fixed
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>