problem with UnboundLocalError: local variable referenced before assignment












0















So I'm testing my knowledge on webscraping by writing this little script (I know some things are redundant, it's just me playing around with things):



def programstart():
import re
from selenium import webdriver
from bs4 import BeautifulSoup

ff=webdriver.Firefox()
ff.get('https://www.ozbargain.com.au/deals')

linkpattern = r'https://www.ozbargain.com.au/node/.*'

donotpassthislink = 'https://www.ozbargain.com.au/node/415763'

#list of found lnks
titlelist =

#loop to find links
while True:
soup = BeautifulSoup(ff.page_source)
raw_links = soup.find_all(name='a', href=re.compile(linkpattern))
for links in raw_links:
titles = ff.find_element_by_xpath("//h2[@class='title']").text
extract = links
if donotpassthislink == extract:
break
else:
pass
titlelist.append(titles)
# Load next page
if donotpassthislink == extract: # stop loop at first movie link from last time.
break
else:
ff.get(ff.find_element_by_xpath("//a[@class='pager-next active']").get_attribute('href'))


programstart()


I get this error:



Traceback (most recent call last):
File "F:/Dropbox/Funfile/ben.py", line 35, in <module>
programstart()
File "F:/Dropbox/Funfile/ben.py", line 29, in programstart
if donotpassthislink == extract: # stop loop at first movie link from last time.
UnboundLocalError: local variable 'extract' referenced before assignment


I find this weird since this loop was used in another script that works perfectly.



So I write a simplified script:



test = 'lets break this sentence up'

list =

def func():
while True:
for i in test:
extract = i
print(extract)
if extract == 'u':
break
else:
pass
list.append(extract)
if extract == 'u':
print('done')
break

func()


And it executes without any issues.
What's going on?










share|improve this question


















  • 1





    If raw_links is an empty list, the for loop is never executed and the variable extract is never defined. As a side note, do not import modules in a function and else: pass is useless.

    – DYZ
    Nov 16 '18 at 4:53













  • Thank you! I put a print(raw_links) after its definition and it came up empty.

    – Paul Zet
    Nov 16 '18 at 5:18
















0















So I'm testing my knowledge on webscraping by writing this little script (I know some things are redundant, it's just me playing around with things):



def programstart():
import re
from selenium import webdriver
from bs4 import BeautifulSoup

ff=webdriver.Firefox()
ff.get('https://www.ozbargain.com.au/deals')

linkpattern = r'https://www.ozbargain.com.au/node/.*'

donotpassthislink = 'https://www.ozbargain.com.au/node/415763'

#list of found lnks
titlelist =

#loop to find links
while True:
soup = BeautifulSoup(ff.page_source)
raw_links = soup.find_all(name='a', href=re.compile(linkpattern))
for links in raw_links:
titles = ff.find_element_by_xpath("//h2[@class='title']").text
extract = links
if donotpassthislink == extract:
break
else:
pass
titlelist.append(titles)
# Load next page
if donotpassthislink == extract: # stop loop at first movie link from last time.
break
else:
ff.get(ff.find_element_by_xpath("//a[@class='pager-next active']").get_attribute('href'))


programstart()


I get this error:



Traceback (most recent call last):
File "F:/Dropbox/Funfile/ben.py", line 35, in <module>
programstart()
File "F:/Dropbox/Funfile/ben.py", line 29, in programstart
if donotpassthislink == extract: # stop loop at first movie link from last time.
UnboundLocalError: local variable 'extract' referenced before assignment


I find this weird since this loop was used in another script that works perfectly.



So I write a simplified script:



test = 'lets break this sentence up'

list =

def func():
while True:
for i in test:
extract = i
print(extract)
if extract == 'u':
break
else:
pass
list.append(extract)
if extract == 'u':
print('done')
break

func()


And it executes without any issues.
What's going on?










share|improve this question


















  • 1





    If raw_links is an empty list, the for loop is never executed and the variable extract is never defined. As a side note, do not import modules in a function and else: pass is useless.

    – DYZ
    Nov 16 '18 at 4:53













  • Thank you! I put a print(raw_links) after its definition and it came up empty.

    – Paul Zet
    Nov 16 '18 at 5:18














0












0








0








So I'm testing my knowledge on webscraping by writing this little script (I know some things are redundant, it's just me playing around with things):



def programstart():
import re
from selenium import webdriver
from bs4 import BeautifulSoup

ff=webdriver.Firefox()
ff.get('https://www.ozbargain.com.au/deals')

linkpattern = r'https://www.ozbargain.com.au/node/.*'

donotpassthislink = 'https://www.ozbargain.com.au/node/415763'

#list of found lnks
titlelist =

#loop to find links
while True:
soup = BeautifulSoup(ff.page_source)
raw_links = soup.find_all(name='a', href=re.compile(linkpattern))
for links in raw_links:
titles = ff.find_element_by_xpath("//h2[@class='title']").text
extract = links
if donotpassthislink == extract:
break
else:
pass
titlelist.append(titles)
# Load next page
if donotpassthislink == extract: # stop loop at first movie link from last time.
break
else:
ff.get(ff.find_element_by_xpath("//a[@class='pager-next active']").get_attribute('href'))


programstart()


I get this error:



Traceback (most recent call last):
File "F:/Dropbox/Funfile/ben.py", line 35, in <module>
programstart()
File "F:/Dropbox/Funfile/ben.py", line 29, in programstart
if donotpassthislink == extract: # stop loop at first movie link from last time.
UnboundLocalError: local variable 'extract' referenced before assignment


I find this weird since this loop was used in another script that works perfectly.



So I write a simplified script:



test = 'lets break this sentence up'

list =

def func():
while True:
for i in test:
extract = i
print(extract)
if extract == 'u':
break
else:
pass
list.append(extract)
if extract == 'u':
print('done')
break

func()


And it executes without any issues.
What's going on?










share|improve this question














So I'm testing my knowledge on webscraping by writing this little script (I know some things are redundant, it's just me playing around with things):



def programstart():
import re
from selenium import webdriver
from bs4 import BeautifulSoup

ff=webdriver.Firefox()
ff.get('https://www.ozbargain.com.au/deals')

linkpattern = r'https://www.ozbargain.com.au/node/.*'

donotpassthislink = 'https://www.ozbargain.com.au/node/415763'

#list of found lnks
titlelist =

#loop to find links
while True:
soup = BeautifulSoup(ff.page_source)
raw_links = soup.find_all(name='a', href=re.compile(linkpattern))
for links in raw_links:
titles = ff.find_element_by_xpath("//h2[@class='title']").text
extract = links
if donotpassthislink == extract:
break
else:
pass
titlelist.append(titles)
# Load next page
if donotpassthislink == extract: # stop loop at first movie link from last time.
break
else:
ff.get(ff.find_element_by_xpath("//a[@class='pager-next active']").get_attribute('href'))


programstart()


I get this error:



Traceback (most recent call last):
File "F:/Dropbox/Funfile/ben.py", line 35, in <module>
programstart()
File "F:/Dropbox/Funfile/ben.py", line 29, in programstart
if donotpassthislink == extract: # stop loop at first movie link from last time.
UnboundLocalError: local variable 'extract' referenced before assignment


I find this weird since this loop was used in another script that works perfectly.



So I write a simplified script:



test = 'lets break this sentence up'

list =

def func():
while True:
for i in test:
extract = i
print(extract)
if extract == 'u':
break
else:
pass
list.append(extract)
if extract == 'u':
print('done')
break

func()


And it executes without any issues.
What's going on?







python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 4:45









Paul ZetPaul Zet

307




307








  • 1





    If raw_links is an empty list, the for loop is never executed and the variable extract is never defined. As a side note, do not import modules in a function and else: pass is useless.

    – DYZ
    Nov 16 '18 at 4:53













  • Thank you! I put a print(raw_links) after its definition and it came up empty.

    – Paul Zet
    Nov 16 '18 at 5:18














  • 1





    If raw_links is an empty list, the for loop is never executed and the variable extract is never defined. As a side note, do not import modules in a function and else: pass is useless.

    – DYZ
    Nov 16 '18 at 4:53













  • Thank you! I put a print(raw_links) after its definition and it came up empty.

    – Paul Zet
    Nov 16 '18 at 5:18








1




1





If raw_links is an empty list, the for loop is never executed and the variable extract is never defined. As a side note, do not import modules in a function and else: pass is useless.

– DYZ
Nov 16 '18 at 4:53







If raw_links is an empty list, the for loop is never executed and the variable extract is never defined. As a side note, do not import modules in a function and else: pass is useless.

– DYZ
Nov 16 '18 at 4:53















Thank you! I put a print(raw_links) after its definition and it came up empty.

– Paul Zet
Nov 16 '18 at 5:18





Thank you! I put a print(raw_links) after its definition and it came up empty.

– Paul Zet
Nov 16 '18 at 5:18












0






active

oldest

votes











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%2f53331592%2fproblem-with-unboundlocalerror-local-variable-referenced-before-assignment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331592%2fproblem-with-unboundlocalerror-local-variable-referenced-before-assignment%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

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly