Reputation: 120
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.
Upvotes: 0
Views: 3140
Reputation: 24038
You have some basic mistakes in your code, it should be like this:
def tempRequest():
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
heading, tree = tempRequest()
sheet = client.open("Database").sheet1
sheet.insert_row(heading, 10)
time.sleep(5)
In your original code you're trying to pass variables to the function before you have defined them in your code. And you're not using your functions return values at all.
Upvotes: 3