Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python get data from div blocks

I am trying to parse div block using Beautiful Soup

Which is

<div class="same-height-left" style="height: 20px;"><span class="value-frame">&nbsp;whatever</span></div>

I want to get [what i expect]:

whatever or <span class="value-frame">&nbsp;whatever</span>

I tried

response = requests.get('http://example.com')
response.raise_for_status()
soup = bs4.BeautifulSoup(response.text)

div = soup.find('div', class_="same-height-left")

Result

None

And

soup = bs4.BeautifulSoup(response.text)

div = soup.find_all('div', class_="same-height-left")

Result

[]

like image 610
dragon Avatar asked Aug 05 '16 13:08

dragon


1 Answers

How about this:

from bs4 import BeautifulSoup

html = """<div class="same-height-left" style="height: 20px;"><span class="value-frame">&nbsp;whatever</span></div>"""

soup = BeautifulSoup(html, 'html.parser')

method1 = soup.find('div').text
method2 = soup.find('div').find('span').text
method3 = soup.find('span', class_='value-frame').text

print 'Result of method 1:' + method1  # prints " whatever"
print 'Result of method 2:' + method2  # prints " whatever"
print 'Result of method 3:' + method3  # prints " whatever"
like image 132
vadimhmyrov Avatar answered Oct 30 '22 19:10

vadimhmyrov