Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search within tags with BeautifulSoup Python

I wanted to search within the tag:

<div id="cmeProductSlatePaginiationTop" class="cmePaginiation">
   <ul>
      <li class="disabled">
      <li class="active">
      <li class="away-1">
      <li>
   </ul>
</div>

Basically, I want to count the number of occurunces of <li ..> in this div. However, when I used beautifulsoup, I can't get the tags in between the div

    soup = BeautifulSoup(resp)
    tags = soup.find('div', attrs = {'class' : 'cmePaginiation'})
    print tags

>>> <div id="cmeProductSlatePaginiationTop" class="cmePaginiation">&nbsp;</div>

Is there a way to count the number of instances of li (In this example 4)?

like image 593
James Hallen Avatar asked May 27 '13 20:05

James Hallen


People also ask

How do I find multiple tags with BeautifulSoup?

In order to use multiple tags or elements, we have to use a list or dictionary inside the find/find_all() function. find/find_all() functions are provided by a beautiful soup library to get the data using specific tags or elements. Beautiful Soup is the python library for scraping data from web pages.

How do you scrape a tag with BeautifulSoup?

Step 1: The first step will be for scraping we need to import beautifulsoup module and get the request of the website we need to import the requests module. Step 2: The second step will be to request the URL call get method.


1 Answers

Use find_all:

div = soup.find('div', id='cmeProductSlatePaginiationTop')
lis = div.find_all('li')
num_lis = len(lis)
like image 122
Blender Avatar answered Oct 18 '22 20:10

Blender