Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68821

Re: help with for loop----python 2.7.2

Newsgroups comp.lang.python
Date 2014-03-23 14:51 -0700
References <84eb4c69-d43d-4777-8a99-34eed9be73d6@googlegroups.com> <03c8b5d0-363e-4287-80d0-a43b0266f2a3@googlegroups.com> <mailman.8418.1395596955.18130.python-list@python.org>
Message-ID <5f8a6cae-4e84-4748-beb1-fd931b187e4e@googlegroups.com> (permalink)
Subject Re: help with for loop----python 2.7.2
From tad na <teddybubu@gmail.com>

Show all headers | View raw


On Sunday, March 23, 2014 12:49:11 PM UTC-5, Ian wrote:
> On Mar 23, 2014 11:31 AM, "tad na" <tedd...@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"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

help with for loop----python 2.7.2 teddybubu@gmail.com - 2014-03-22 04:21 -0700
  Re: help with for loop----python 2.7.2 Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-22 06:00 -0600
  Re: help with for loop----python 2.7.2 tad na <teddybubu@gmail.com> - 2014-03-23 10:29 -0700
    Re: help with for loop----python 2.7.2 tad na <teddybubu@gmail.com> - 2014-03-23 10:30 -0700
      Re: help with for loop----python 2.7.2 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-23 17:40 +0000
        Re: help with for loop----python 2.7.2 tad na <teddybubu@gmail.com> - 2014-03-23 10:49 -0700
      Re: help with for loop----python 2.7.2 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-23 18:43 +0000
    Re: help with for loop----python 2.7.2 Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-23 11:49 -0600
      Re: help with for loop----python 2.7.2 tad na <teddybubu@gmail.com> - 2014-03-23 14:51 -0700
        Re: help with for loop----python 2.7.2 Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-23 16:44 -0600
  Re: help with for loop----python 2.7.2 Ben Finney <ben+python@benfinney.id.au> - 2014-03-24 09:39 +1100

csiph-web