How to divide an element inside another div element using beautifulsoup?

This is an example of HTML code:

   <div class="cb-col cb-col-25 cb-mtch-blk"><a class="cb-font-12" href="/live-cricket-scores/16947/ind-vs-ban-only-test-bangladesh-tour-of-india-2017" target="_self" title="India v Bangladesh - Only Test">
<div class="cb-hmscg-bat-txt cb-ovr-flo ">
<div class="cb-ovr-flo cb-hmscg-tm-nm">BAN</div>
<div class="cb-ovr-flo" style="display:inline-block; width:140px">322/6 (104.0 Ovs)</div>
</div>

I want to extract text from BAN and 322/6 (104.0 Ovs) from the above html parsing. I do like this:

soup = BeautifulSoup(html)
div_class = soup.findAll('div',class_='cb-col cb-col-25 cb-mtch-blk')
for each in div_class:
    #I want to get those texts from variable 'each'

How can i do this?

+4
source share
2 answers

You can use multiple css selectors with BeautifulSoup4:

>>> from bs4 import BeautifulSoup
>>> html = ...  # the html provided in the question
>>> soup = BeautifulSoup(html, 'lxml')
>>> name, size = soup.select('div.cb-hmscg-bat-txt.cb-ovr-flo div')
>>> name.text
u'BAN'
>>> size.text
u'322/6 (104.0 Ovs)'
+3
source

eachmeans the HTML code you provided, you should go to the next tag divand get all the text stripped_strings.

div_class = soup.findAll('div',class_='cb-col cb-col-25 cb-mtch-blk')
for each in div_class:
    name, size = each.div.stripped_strings
    print(name, size)

of

BAN 322/6 (104.0 Ovs)
+1
source

Source: https://habr.com/ru/post/1669496/


All Articles