Im doing some python web content requests and I want to make some functions in my code, but there is one error and I dont know why it´s showing. My code looks like this:
def tempRequest(tree, heading):
page = requests.get("http://10.0.0.3/admin/speedtest.php")
tree = html.fromstring(page.content)
heading = tree.xpath('//a[@id="temperature"]/text()')
return heading, tree
tempRequest(tree, heading)
heading = tree.xpath('//a[@id="temperature"]/text()')
sheet = client.open("Database").sheet1
sheet.insert_row(heading, 10)
time.sleep(5)
tempRequest(tree, heading) NameError: name 'tree' is not defined
Could you guys please help me? Thanks.
treein the global scope to pass to the function. How can you passtreeintempRequest(tree, heading)when it doesn't exist prior to calling the function? Even without knowledge of python scoping, this is illogical.page = requests.get("http://10.0.0.3/admin/speedtest.php")tree = html.fromstring(page.content)heading = tree.xpath('//a[@id="temperature"]/text()')tempRequest(tree, heading)tempRequestdoesn't need any arguments, though.