help with for loop----python 2.7.2

tad na teddybubu at gmail.com
Sun Mar 23 18:30:27 CET 2014


On Sunday, March 23, 2014 12:29:40 PM UTC-5, tad na wrote:
> On Saturday, March 22, 2014 6:21:30 AM UTC-5, tad na wrote:
> 
> > I am trying to get all the element data from the rss below.
> 
> > 
> 
> > The only thing I am pulling is the first element.
> 
>  
> 
> > I don't understand why the for loop does not go through the entire rss.
> 
> 
> 
> > Here is my code....
> 
> > try:
> 
> >     from urllib2 import urlopen
> 
> > except ImportError:
> 
> >     from urllib.request import urlopen  
> 
> > from bs4 import BeautifulSoup 
> 
> > soup = BeautifulSoup(urlopen('http://bl.ocks.org/mbostock.rss'))
> 
> > #print soup.find_all('item')
> 
> > #print (soup)
> 
> > for item in soup.find_all('item'):
> 
> > #for item in soup:    
> 
> >     title = soup.find('title').text    
> 
> >     link = soup.find('link').text
> 
> >     item = soup.find('item').text
> 
> >     print item
> 
> >     print title
> 
> >     print link
> 
> OK . second problem :)
> 
> I can print the date.  not sure how to do this one..
> 
> try:
> 
>     from urllib2 import urlopen
> 
> except ImportError:
> 
>     from urllib.request import urlopen 
> 
> import urllib2
> 
> from bs4 import BeautifulSoup
> 
> 
> 
> soup = BeautifulSoup(urlopen('http://bl.ocks.org/mbostock.rss'))
> 
> #print soup.find_all('item')
> 
> #print (soup)
> 
> data = soup.find_all("item")
> 
> 
> 
> x=0
> 
> for item in soup.find_all('item'):
> 
>     title = item.find('title').text    
> 
>     link = item.find('link').text
> 
>     date = item.find('pubDate')
> 
>    # print date
> 
>     print('+++++++++++++++++')
> 
>     print data[x].title.text
> 
>     print data[x].link.text
> 
>     print data[x].guid.text
> 
>     print data[x].pubDate
> 
>     x = x + 1

meant to say CANNOT print the date



More information about the Python-list mailing list