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


Groups > comp.lang.python > #68818

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

References <84eb4c69-d43d-4777-8a99-34eed9be73d6@googlegroups.com> <03c8b5d0-363e-4287-80d0-a43b0266f2a3@googlegroups.com>
Date 2014-03-23 11:49 -0600
Subject Re: help with for loop----python 2.7.2
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.8418.1395596955.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Mar 23, 2014 11:31 AM, "tad na" <teddybubu@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.

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