beautifulsoup - Python, Beautiful Soup: how to get the desired element -
i trying arrive element, parsing source code of site. snippet part i'm trying parse (here until friday), same days of week
<div id="intforecast"> <h2>forecast rome</h2> <table cellspacing="0" cellpadding="0" id="nonca"> <tr> <td onclick="showdetails('1');return false" id="day1" class="on"> <span>thursday</span> <div class="inticon"><img src="http://icons.wunderground.com/graphics/conds/2005/sunny.gif" alt="sunny" /></div> <div>clear</div> <div><span class="hi">h <span>22</span>°</span> / <span class="lo">l <span>11</span>°</span></div> </td> <td onclick="showdetails('2');return false" id="day2" class="off"> <span>friday</span> <div class="inticon"><img src="http://icons.wunderground.com/graphics/conds/2005/partlycloudy.gif" alt="partlycloudy" /></div> <div>partly cloudy</div> <div><span class="hi">h <span>21</span>°</span> / <span class="lo">l <span>15</span>°</span></div> </td> </tr> </table> </div>
....and on days
actually got result in ugly way think:
forecastfriday= soup.find('div',text='friday').findnext('div').findnext('div').string
now, can see go deep down elements repeating .findnext('div')
and arrive @ .string
i want information "partly cloudy" of friday
so more pythonic way this? thanks!
simply find of <td>
s , iterate on them:
soup = beautifulsoup(your_html) div = soup('div',{'id':'intforecast'})[0] tds = div.find('table').findall('td') td in tds: day = td('span')[0].text forecast = td('div')[1].text print day, forecast
Comments
Post a Comment