title: Python Blog RSS Feed Scraping BeautifulSoup Output to .txt Files
body: <p>Apologies in advance for the long block of code following. I'm new to BeautifulSoup, but found there were some useful tutorials using it to scrape RSS feeds for blogs. Full disclosure: this is code adapted from this video tutorial which has been immensely helpful in getting this off the ground: <a href="http://www.youtube.com/watch?v=Ap_DlSrT-iE" rel="nofollow">http://www.youtube.com/watch?v=Ap_DlSrT-iE</a>.</p>
  
  <p>Here's my problem: the video does a great job of showing how to print the relevant content to the console. I need to write out each article's text to a separate .txt file and save it to some directory (right now I'm just trying to save to my Desktop). I know the problem lies i the scope of the two for-loops near the end of the code (I've tried to comment this for people to see quickly--it's the last comment beginning # Here's where I'm lost...), but I can't seem to figure it out on my own. </p>
  
  <p>Currently what the program does is takes the text from the last article read in by the program and writes that out to the number of .txt files that are indicated in the variable <code>listIterator</code>. So, in this case I believe there are 20 .txt files that get written out, but they all contain the text of the last article that's looped over. What I want the program to do is loop over each article and print the text of each article out to a separate .txt file. Sorry for the verbosity, but any insight would be really appreciated.</p>
  
  <pre><code>from urllib import urlopen
  from bs4 import BeautifulSoup
  import re
  
  # Read in webpage.
  webpage = urlopen('http://talkingpointsmemo.com/feed/livewire').read()
  
  # On RSS Feed site, find tags for title of articles and 
  # tags for article links to be downloaded.
  
  patFinderTitle = re.compile('&lt;title&gt;(.*)&lt;/title&gt;')
  patFinderLink = re.compile('&lt;link rel.*href="(.*)"/&gt;')
  
  # Find the tags listed in variables above in the articles.
  findPatTitle = re.findall(patFinderTitle, webpage)
  findPatLink = re.findall(patFinderLink, webpage)
  
  # Create a list that is the length of the number of links
  # from the RSS feed page. Use this to iterate over each article,
  # read it in, and find relevant text or &lt;p&gt; tags.
  listIterator = []
  listIterator[:] = range(len(findPatTitle))
  
  for i in listIterator:
      # Print each title to console to ensure program is working. 
      print findPatTitle[i]
  
      # Read in the linked-to article.
      articlePage = urlopen(findPatLink[i]).read()
  
      # Find the beginning and end of articles using tags listed below.
      divBegin = articlePage.find("&lt;div class='story-teaser'&gt;")
      divEnd = articlePage.find("&lt;footer class='article-footer'&gt;")
  
      # Define article variable that will contain all the content between the 
      # beginning of the article to the end as indicated by variables above.
      article = articlePage[divBegin:divEnd]
  
      # Parse the page using BeautifulSoup
      soup = BeautifulSoup(article)
  
      # Compile list of all &lt;p&gt; tags for each article and store in paragList
      paragList = soup.findAll('p')
  
      # Create empty string to eventually convert items in paragList to string to 
      # be written to .txt files.
      para_string = ''
  
      # Here's where I'm lost and have some sort of scope issue with my for-loops.
      for i in paragList:
          para_string = para_string + str(i)
          newlist = range(len(findPatTitle))
          for i in newlist:
              ofile = open(str(listIterator[i])+'.txt', 'w')
              ofile.write(para_string)
              ofile.close()
  </code></pre>