why i am getting UnboundLocalError












-1














import requests
from bs4 import BeautifulSoup
from random import choice

def get_proxy():
url = "https://free-proxy-list.net/"

r = requests.get(url)
soup =BeautifulSoup(r.content , 'lxml')
return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}

def proxy_request(request_type, url, **kwargs):
while 1:
try:
proxy = get_proxy()
print("using proxy: {}".format(proxy))
r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
num = soup.find_all('h1')
print (num.text())
break
except:
pass

return r
r = proxy_request('get',"https://en.wikipedia.org/wiki/Main_Page")
print (r.text())


and i am getting this error










share|improve this question




















  • 1




    You haven't included the error. Please show that and the traceback.
    – roganjosh
    Nov 22 at 18:09






  • 2




    Side note: I hope never to have to maintain return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}
    – roganjosh
    Nov 22 at 18:12










  • using proxy: {'https': '131.161.210.40:32821'} Traceback (most recent call last): File "/Users/imac/Documents/trying auto ips.py", line 24, in <module> r.text AttributeError: 'NoneType' object has no attribute 'text' >>> ============= this is the error
    – muhammad azain
    Nov 22 at 18:12
















-1














import requests
from bs4 import BeautifulSoup
from random import choice

def get_proxy():
url = "https://free-proxy-list.net/"

r = requests.get(url)
soup =BeautifulSoup(r.content , 'lxml')
return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}

def proxy_request(request_type, url, **kwargs):
while 1:
try:
proxy = get_proxy()
print("using proxy: {}".format(proxy))
r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
num = soup.find_all('h1')
print (num.text())
break
except:
pass

return r
r = proxy_request('get',"https://en.wikipedia.org/wiki/Main_Page")
print (r.text())


and i am getting this error










share|improve this question




















  • 1




    You haven't included the error. Please show that and the traceback.
    – roganjosh
    Nov 22 at 18:09






  • 2




    Side note: I hope never to have to maintain return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}
    – roganjosh
    Nov 22 at 18:12










  • using proxy: {'https': '131.161.210.40:32821'} Traceback (most recent call last): File "/Users/imac/Documents/trying auto ips.py", line 24, in <module> r.text AttributeError: 'NoneType' object has no attribute 'text' >>> ============= this is the error
    – muhammad azain
    Nov 22 at 18:12














-1












-1








-1







import requests
from bs4 import BeautifulSoup
from random import choice

def get_proxy():
url = "https://free-proxy-list.net/"

r = requests.get(url)
soup =BeautifulSoup(r.content , 'lxml')
return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}

def proxy_request(request_type, url, **kwargs):
while 1:
try:
proxy = get_proxy()
print("using proxy: {}".format(proxy))
r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
num = soup.find_all('h1')
print (num.text())
break
except:
pass

return r
r = proxy_request('get',"https://en.wikipedia.org/wiki/Main_Page")
print (r.text())


and i am getting this error










share|improve this question















import requests
from bs4 import BeautifulSoup
from random import choice

def get_proxy():
url = "https://free-proxy-list.net/"

r = requests.get(url)
soup =BeautifulSoup(r.content , 'lxml')
return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}

def proxy_request(request_type, url, **kwargs):
while 1:
try:
proxy = get_proxy()
print("using proxy: {}".format(proxy))
r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
num = soup.find_all('h1')
print (num.text())
break
except:
pass

return r
r = proxy_request('get',"https://en.wikipedia.org/wiki/Main_Page")
print (r.text())


and i am getting this error







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 18:06









Austin

9,4023828




9,4023828










asked Nov 22 at 18:04









muhammad azain

1




1








  • 1




    You haven't included the error. Please show that and the traceback.
    – roganjosh
    Nov 22 at 18:09






  • 2




    Side note: I hope never to have to maintain return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}
    – roganjosh
    Nov 22 at 18:12










  • using proxy: {'https': '131.161.210.40:32821'} Traceback (most recent call last): File "/Users/imac/Documents/trying auto ips.py", line 24, in <module> r.text AttributeError: 'NoneType' object has no attribute 'text' >>> ============= this is the error
    – muhammad azain
    Nov 22 at 18:12














  • 1




    You haven't included the error. Please show that and the traceback.
    – roganjosh
    Nov 22 at 18:09






  • 2




    Side note: I hope never to have to maintain return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}
    – roganjosh
    Nov 22 at 18:12










  • using proxy: {'https': '131.161.210.40:32821'} Traceback (most recent call last): File "/Users/imac/Documents/trying auto ips.py", line 24, in <module> r.text AttributeError: 'NoneType' object has no attribute 'text' >>> ============= this is the error
    – muhammad azain
    Nov 22 at 18:12








1




1




You haven't included the error. Please show that and the traceback.
– roganjosh
Nov 22 at 18:09




You haven't included the error. Please show that and the traceback.
– roganjosh
Nov 22 at 18:09




2




2




Side note: I hope never to have to maintain return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}
– roganjosh
Nov 22 at 18:12




Side note: I hope never to have to maintain return{'https': choice(list(map(lambda x:x[0]+':'+x[1],list(zip(map(lambda x:x.text, soup.findAll('td')[::8]),map(lambda x:x.text, soup.findAll('td')[1::8]))))))}
– roganjosh
Nov 22 at 18:12












using proxy: {'https': '131.161.210.40:32821'} Traceback (most recent call last): File "/Users/imac/Documents/trying auto ips.py", line 24, in <module> r.text AttributeError: 'NoneType' object has no attribute 'text' >>> ============= this is the error
– muhammad azain
Nov 22 at 18:12




using proxy: {'https': '131.161.210.40:32821'} Traceback (most recent call last): File "/Users/imac/Documents/trying auto ips.py", line 24, in <module> r.text AttributeError: 'NoneType' object has no attribute 'text' >>> ============= this is the error
– muhammad azain
Nov 22 at 18:12












1 Answer
1






active

oldest

votes


















0














The problem is your return is in your while loop. When you break, you skip return, therefore r is None:



def proxy_request(request_type, url, **kwargs):
while 1:
try:
proxy = get_proxy()
print("using proxy: {}".format(proxy))
r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
num = soup.find_all('h1')
print (num.text())
break
except:
pass

return num # return it here, outside of the while loop


You'll also want to consider the case where num might not be found and get a case to break out to avoid infinite loops






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436233%2fwhy-i-am-getting-unboundlocalerror%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The problem is your return is in your while loop. When you break, you skip return, therefore r is None:



    def proxy_request(request_type, url, **kwargs):
    while 1:
    try:
    proxy = get_proxy()
    print("using proxy: {}".format(proxy))
    r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
    num = soup.find_all('h1')
    print (num.text())
    break
    except:
    pass

    return num # return it here, outside of the while loop


    You'll also want to consider the case where num might not be found and get a case to break out to avoid infinite loops






    share|improve this answer


























      0














      The problem is your return is in your while loop. When you break, you skip return, therefore r is None:



      def proxy_request(request_type, url, **kwargs):
      while 1:
      try:
      proxy = get_proxy()
      print("using proxy: {}".format(proxy))
      r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
      num = soup.find_all('h1')
      print (num.text())
      break
      except:
      pass

      return num # return it here, outside of the while loop


      You'll also want to consider the case where num might not be found and get a case to break out to avoid infinite loops






      share|improve this answer
























        0












        0








        0






        The problem is your return is in your while loop. When you break, you skip return, therefore r is None:



        def proxy_request(request_type, url, **kwargs):
        while 1:
        try:
        proxy = get_proxy()
        print("using proxy: {}".format(proxy))
        r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
        num = soup.find_all('h1')
        print (num.text())
        break
        except:
        pass

        return num # return it here, outside of the while loop


        You'll also want to consider the case where num might not be found and get a case to break out to avoid infinite loops






        share|improve this answer












        The problem is your return is in your while loop. When you break, you skip return, therefore r is None:



        def proxy_request(request_type, url, **kwargs):
        while 1:
        try:
        proxy = get_proxy()
        print("using proxy: {}".format(proxy))
        r = requests.request(request_type, url, proxies=proxy, timeout=7, **kwargs)
        num = soup.find_all('h1')
        print (num.text())
        break
        except:
        pass

        return num # return it here, outside of the while loop


        You'll also want to consider the case where num might not be found and get a case to break out to avoid infinite loops







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 18:30









        C.Nivs

        1,8271414




        1,8271414






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436233%2fwhy-i-am-getting-unboundlocalerror%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo