Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get div from HTML with Python

Tags:

python

html

regex

I want to get a value inside certain div from a HTML page

    <div class="well credit">

      <div class="span2">
          <h3><span>
              $ 5.402 
          </span></h3>
      </div>

    </div>

I've done it with regular expressions ( re.seach() ) but it take too long to find the div since it's a huge html.

Is there a way to do this faster but with no external libraries?

Thanks

like image 792
JoseMiguel Avatar asked Jan 07 '14 15:01

JoseMiguel


1 Answers

I would use BeautifulSoup!

to get everything with <div> tag just do:

soup = BeautifulSoup(html)#make soup that is parse-able by bs
soup.findAll('div') 

to get the value inside of span you could do:

soup.find('span').get_text()

there are tons of differnt methods of getting the informaton you need

Good Luck hope this helps!

like image 161
Serial Avatar answered Nov 13 '22 18:11

Serial