pyxe
pyxe

Reputation: 13

beautifulsoup not returning span results

I'm learning bs4 and trying to scrape the span tag data from this website put them in a list but no results are returned what am I doing wrong?

import requests
import bs4

root_url = 'http://www.timeanddate.com'
index_url = root_url + '/astronomy/tonga/nukualofa'

response = requests.get(index_url)
soup = bs4.BeautifulSoup(response.text)
spans = soup.find_all('span', attrs={'id':'qfacts'})
for span in spans:
    print span.string

All span data from the webpage is in between this tag

<div class="five columns" id="qfacts">
    <p><span class="four">Current Time:</span> <span id="smct">16 Mar 2015 at
12:53:50 p.m.</span></p><br>

    <p><span class="four">Sunrise Today:</span> <span class="three">6:43
a.m.</span> <span class="comp sa8" title="Map direction East">↑</span> 93°
East<br>
    <span class="four">Sunset Today:</span> <span class="three">6:56
p.m.</span> <span class="comp sa24" title="Map direction West">↑</span>
268° West</p><br>

    <p><span class="four">Moonrise Today:</span> <span class="three">1:55
a.m.</span> <span class="comp sa10" title="Map direction East">↑</span>
108° East<br>
<span class="four">Moonset Today:</span> <span class="three">3:17
p.m.</span> <span class="comp sa22" title="Map direction West">↑</span>
253° West</p><br>

    <p><span class="four">Daylight Hours:</span> <span title=
"The current day is 12 hours, 13 minutes long which is 1m 13s shorter than yesterday.">
12 hours, 13 minutes (-1m 13s)</span></p>
</div>

Upvotes: 1

Views: 1021

Answers (1)

Jared
Jared

Reputation: 26397

Subtle mistake is that you are searching for span tags that have an id of "facts" when what you really want is to search for span tags within the div having that id.

Replace,

spans = soup.find_all('span', attrs={'id':'qfacts'})

with,

div = soup.find('div', attrs={'id': 'qfacts'})  # <-- div not span
spans = div.find_all('span')  # <-- now find the spans inside

Were you looking for divs having some class you would want to iterate those divs and find all spans inside but this is an id so a single find call should suffice.

Upvotes: 1

Related Questions