dragon dragon - 3 months ago 8
Python Question

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


[]

Answer

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"
Comments