urllib, or other package, for reading online text

Skip to first unread message

Kaanchan Adhikary

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

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.

import urllib

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

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?


Michael Cimino

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

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
= 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
0 new messages