help with for loop----python 2.7.2

Ian Kelly ian.g.kelly at
Sat Mar 22 13:00:47 CET 2014

On Sat, Mar 22, 2014 at 5:21 AM,  <teddybubu at> 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....


> 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

The three find method calls in the for loop are searching from the
document root (the "soup" variable), not from the item you're
currently iterating at.  Try changing these to calls of item.find. And
note that calling one of the results "item" will replace the loop
variable.  That won't affect the iteration, but it's bad practice to
refer to two different things by the same local name.

More information about the Python-list mailing list