urllib / urequests for Micropython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
mbevilacqua
Posts: 6
Joined: Sat May 07, 2016 10:30 pm

Re: urllib / urequests for Micropython

Post by mbevilacqua » Sun Oct 09, 2016 3:45 pm

I'm facing this exact same error trying: f = urllib.urlopen('http://api.randomstuff.com')

mamvcivm
Posts: 1
Joined: Tue Jan 02, 2018 11:57 pm

Re: urllib / urequests for Micropython

Post by mamvcivm » Wed Jan 03, 2018 12:19 am

Hi, I've recently installed Micropython 1.9.3 onto a NodeMCU device, and I'm currently trying to get a Python file I've previously written for a Raspberry Pi to run. I've imported urllib.urequest and successfully ran
f = urllib.urequest.urlopen("http://aurorawatch.lancs.ac.uk/api/0.1/activity.txt"), however, I don't know how to then manipulate the downloaded string. In Python I would use lines = f.readlines() but this results in an error:-

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'socket' object has no attribute 'readlines'

Can you please advise how I can manipulate the text string downloaded from the above site?

Thanks,

Andrew

UPDATE: I've discovered the following:-

import urequests as requests
f = requests.get("http://aurorawatch.lancs.ac.uk/api/0.1/activity.txt")
myfile = (f.text)
print (myfile)
f.close()

will save the contents of the above txt file to "myfile", now I need to figure out how to manipulate myfile so that just the final numerical value of the last line is extracted...

Post Reply