how to use a variable in requests library of python
Question: I'm trying to make a script which reads crunchyroll's rss and visits the LINK
in the latest upload and downloads subs from it.. the process goes like : 1.)
Read The latest episode link from RSS. 2.) Go to the link 3.) In the source
code, look for text "ssid". 4.) Get the 6 characters of the ssid. 5.) Then
append those characters at the end of this like
"[http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=](http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=)"
and save the xml page.

My script works half way...

My Code:-

    
    
    import feedparser
    import webbrowser
    import os
    import subprocess  
    import re
    import urllib
    import urllib2
    from urllib2 import urlopen
    from bs4 import BeautifulSoup
    import requests
    import cookielib
    
    
    feed = feedparser.parse('http://www.crunchyroll.com/rss/anime')  #checks the RSS
    url = feed['entries'][0]['link'] + '?p720=1'         # get's the link from latest release and appends some character for the 720p resolution of the link.
    
    # Now, here, I'm writing this URL to a text file and then read from the text file
    
    file = open("newfile.txt", "w")
    file.write(url)
    file.close()
    
    file = open('newfile.txt', 'r')
    #print file.read()
    lobo = file.read()
    print lobo
    
    # Now, I put the URL that is being read from file in requests to go to the link. Everything works fine till here.
    
    r = requests.get(lobo)
    soup = BeautifulSoup(r.text)
    print soup.title
    webbrowser.open_new_tab(lobo)
    subtitles = soup.findAll('span',{'class':'showmedia-subtitle-text'})
    for ssid in subtitles:
      x = ssid.find_all('a', limit=1)
    for a in x:
      print a['href'][-6:]
    
    xmlLink = 'http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=' + a['href'][-6:]
    #webbrowser.open_new_tab(xmlLink)
    print xmlLink
    

Now, I get error that 'a' in this xmlLink is not defined.

But, there's a twist to it... if I put the direct http link in "r =
requests.get(lobo)" .. everything works like it is supposed to.But, if I use
this variable.. it's not working.

Any help would be appreciated.Thank You


Answer: It looks like the `a` variable is defined inside of the `for` loop but the
`xmlLink` variable is not. Try indenting the `xmlLink` line in to match the
indentation level of the `for` loop. For example:

    
    
    for a in x:
      print a['href'][-6:]
    
      xmlLink = 'http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=' + a['href'][-6:]
      #webbrowser.open_new_tab(xmlLink)
      print xmlLink
    

