Implement a def within an if [PYTHON]

0

Hello this is the "part 2" of my question, what I want to do is that the user when you enter "1" that scrip is executed, first I will pass the code where is the if

eligio = raw_input("""

1)Extraccion de html

2)otro

3)otro

Elige :><: """)

if eligio =="1": #Aqui es donde empezaria el otro codigo

After that if I want the following code to extract the html from a page

# -*- coding: utf-8 -*-

import time, urllib2
import os
import sys
def html(link): # Definimos  
    try:
        res = urllib2.Request(link)
        return urllib2.urlopen(res).read()

    except Exception, e:
        print """Error    """
        time.sleep(3)
        os.system("rm" + " " +name_of_files)
        os.system("clear")
        return ''

os.system("clear")
http = "http://"
link = http+raw_input(""" Ingresa la url sin http : """)
os.system("clear")

name_of_files = raw_input(""" Ingresa el nombre del archivo mas su  extension : """)
archivo = open(name_of_files, "a+") 
archivo.write(html(link))

As in the previous question they told me to call the def function, I did it but I set this error

File "Web_T.py", line 20, in html html (1) RuntimeError: maximum recursion ** depth exceeded **

They also explained that error to me, thank you.

    
asked by Snoop13 23.01.2017 в 20:38
source

2 answers

1

I do not know how you are implementing it but you should not give problems simply by embedding the code inside the conditional, the following code works perfectly by passing the link to this same question:

# -*- coding: utf-8 -*-

import time, urllib2
import os
import sys


eligio = raw_input("""

1)Extraccion de html

2)otro

3)otro

Elige :><: """)

if eligio =="1":
    def html(link): # Definimos
        try:
            res = urllib2.Request(link)
            return urllib2.urlopen(res).read()

        except Exception, e:
            print """Error    """
            time.sleep(3)
            os.system("rm" + " " +name_of_files)
            os.system("clear")
            return ''

    os.system("clear")
    http = "http://"
    link = http+raw_input(""" Ingresa la url sin http : """)
    os.system("clear")

    name_of_files = raw_input(""" Ingresa el nombre del archivo mas su  extension : """)
    archivo = open(name_of_files, "a+")
    archivo.write(html(link))

elif eligio == '2':
    pass
elif eligio == '3':
    pass

Try to see if it generates an error.

    
answered by 23.01.2017 / 21:17
source
2

If you are going to occupy the method anyway, why do not you just call it when the user chooses 1?
Anyway, when loading the script, the method will be generated, only it will be used if it is necessary.

# -*- coding: utf-8 -*-

import time, urllib2
import os
import sys

def html(link): # Definimos
    try:
        res = urllib2.Request(link)
        return urllib2.urlopen(res).read()
    except Exception, e:
        print """Error    """
        time.sleep(3)
        os.system("rm" + " " +name_of_files)
        os.system("clear")
        return ''


eligio = raw_input("""

1)Extraccion de html

2)otro

3)otro

Elige :><: """)



if eligio =="1":
    os.system("clear")
    http = "http://"
    link = http+raw_input(""" Ingresa la url sin http : """)
    os.system("clear")

    name_of_files = raw_input(""" Ingresa el nombre del archivo mas su  extension : """)
    archivo = open(name_of_files, "a+")
    archivo.write(html(link))

elif eligio == '2':
    pass
elif eligio == '3':
    pass
    
answered by 24.01.2017 в 21:35