help with for loop----python 2.7.2

tad na teddybubu at gmail.com
Sun Mar 23 13:29:40 EDT 2014


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



More information about the Python-list mailing list