Sorry if this question has been asked before. I looked around for quite some time, and I did not find a solution.
So, I created a class in the file ResourceOpen.py
class ResourceOpen():
import urllib.request
def __init__(self, source):
try:
page = urllib.request.urlopen(source)
self.text = page.read().decode("utf8")
except ValueError:
print ("Woops! Can't find the URL.")
self.text = ''
def getText(self):
return self.text
I would like to use this class in another youTubeCommentReader.py program ...
import ResourceOpen
import urllib.request
pageToOpen = "http://www.youtube.com"
resource = ResourceOpen.ResourceOpen(pageToOpen)
text = resource.getText()
Whenever I try to start youTubeCommentReader, I get an error message:
Traceback
<module> D:\myPythonProgs\youTubeCommentReader.py
__init__ D:\myPythonProgs\ResourceOpen.py
NameError: global name 'urllib' is not defined
What am I doing wrong? In addition, I should note that ResourceOpen.py works fine when I access a class in the same file.
source
share