AttributeError(“‘str’ object has no attribute ‘read'”)

In Python I’m getting an error: Exception: (<type ‘exceptions.AttributeError’>, AttributeError(“‘str’ object has no attribute ‘read'”,), <traceback object at 0x1543ab8>) Given python code: def getEntries (self, sub): url=”http://www.reddit.com/” if (sub != ”): url += ‘r/’ + sub request = urllib2.Request (url + ‘.json’, None, {‘User-Agent’ : ‘Reddit desktop client by /user/RobinJ1995/’}) response = urllib2.urlopen (request) jsonStr … Read more

Downloading a picture via urllib and python

So I’m trying to make a Python script that downloads webcomics and puts them in a folder on my desktop. I’ve found a few similar programs on here that do something similar, but nothing quite like what I need. The one that I found most similar is right here (http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images). I tried using this code: … Read more

How to download image using requests

I’m trying to download and save an image from the web using python’s requests module. Here is the (working) code I used: img = urllib2.urlopen(settings.STATICMAP_URL.format(**data)) with open(path, ‘w’) as f: f.write(img.read()) Here is the new (non-working) code using requests: r = requests.get(settings.STATICMAP_URL.format(**data)) if r.status_code == 200: img = r.raw.read() with open(path, ‘w’) as f: f.write(img) … Read more

Import error: No module name urllib2

Here’s my code: import urllib2.request response = urllib2.urlopen(“http://www.google.com”) html = response.read() print(html) Any help? 10 s 10 As stated in the urllib2 documentation: The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3. So you … Read more

What are the differences between the urllib, urllib2, urllib3 and requests module?

In Python, what are the differences between the urllib, urllib2, urllib3 and requests modules? Why are there three? They seem to do the same thing… 1Best Answer 11 I know it’s been said already, but I’d highly recommend the requests Python package. If you’ve used languages other than python, you’re probably thinking urllib and urllib2 … Read more