help with for loop----python 2.7.2

Ian Kelly ian.g.kelly at gmail.com
Sun Mar 23 18:49:11 CET 2014


On Mar 23, 2014 11:31 AM, "tad na" <teddybubu 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140323/9ffefd54/attachment.html>


More information about the Python-list mailing list