help with for loop----python 2.7.2

tad na teddybubu at gmail.com
Sun Mar 23 17:51:19 EDT 2014


On Sunday, March 23, 2014 12:49:11 PM UTC-5, Ian wrote:
> On Mar 23, 2014 11:31 AM, "tad na" <tedd... at gmail.com> wrote:
 
> > OK . second problem :)

> > I can print the date.  not sure how to do this one..
> Why not? What happens when you try?
> > 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
> data[x] should be the same object as item, no? If you want to keep track of the current iteration index, a cleaner way to do that is by using enumerate:

>     for x, item in enumerate(soup.find_all('item')):
 
> As far as printing the pubDate goes, why not start by getting its text property as you do with the other tags? From there you can either print the string out directly or parse it into a datetime object.

This is the error I get with 
1. print data[x].pubDate.text
    AttributeError: 'NoneType' object has no attribute 'text'
2. print data[x].pubDate
    It results in "None"





More information about the Python-list mailing list