Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

urllib, or other package, for reading online text

55 views
Skip to first unread message

Kaanchan Adhikary

unread,
Oct 8, 2019, 12:10:28 PM10/8/19
to Skulpt
Hi,

I'm using repl.it (https://repl.it/@KaanchanAdhikar/KnightsTour) to leverage the Turtle implementation, which uses Skulpt. I want to read data from a URL/file online, but can't find any code that works.

Example:
import urllib

f = urllib.read(link)
myfile = f.read()
print(myfile)

I get the following error in the console:
AttributeError: '<invalid type>' object has no attribute 'read' on 

If I try to use urllib2, the console shows:

NotImplementedError: urllib2 is not yet implemented in Skulpt on line 1

Any ideas how I can achieve reading URL content from Skulpt?

Thanks

Michael Cimino

unread,
Dec 31, 2019, 3:49:58 PM12/31/19
to Skulpt
Hello,

One key to reading URLs in a Python Turtle Repl (using Skulpt) is to use
import urllib.request

The second key ... is that it because of cross-origin restrictions, this library can only open https URLs that are either:
According to Wikipedia:
Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served.

import urllib.request
my_file
= urllib.request.urlopen("CORS-enabled-HTTPS-URL-here")
print my_file.read()

Another way to bypass the CORS restriction is to use a CORS proxy service to read the restricted resource.

I hope that helps.
Reply all
Reply to author
Forward
0 new messages