I imported urllib module and tried to use urllib.urlretrieve() function with some arguments. Then it got the error "attributeError: module 'urllib' has no attribute 'urlretrieve'"
I tried with both python 2x and 3x.
import urllib
import json
import requests
count=1
req=requests.get("http://meme-api.herokuapp.com/gimme")
json_data = json.loads(req.text)
imgs="memes-"+str(count)+".jpg"
print(imgs)
urllib.urlretrieve(json_data["url"],imgs)
print(imgs + "is saved")
"attributeError: module 'urllib' has no attribute 'urlretrieve'"
use urlib.request.urlretrieve() instead of urllib.retrieve()
import urllib.request
import json
import requests
count=1
req=requests.get("http://meme-api.herokuapp.com/gimme")
json_data = json.loads(req.text)
imgs="memes-"+str(count)+".jpg"
print(imgs)
urllib.request.urlretrieve(json_data["url"],imgs)
print(imgs + "is saved")
Related
I'm trying to change my python code from 2.7 to 3.6
So, I'm not familiar to python but I have error with urllib2
I have this error
Error Contents: name 'urllib2' is not defined
So I do this:
from urllib.request import urlopen
This is maybe ok, because urllib2 doesn't work on phyton 3?
But I have this:
class NoRedirection(urllib2.HTTPErrorProcessor):
def http_response(self, request, response):
return response
https_response = http_response
What I tried to change
class NoRedirection(urlopen.HTTPErrorProcessor):
But does't work. How to fix this?
**AttributeError: 'function' object has no attribute 'HTTPErrorProcessor'**
There is a separate module for errors found here. What you want to do is something along these lines
from urllib.error import HTTPError
class NoRedirection(HTTPError):
...
Hello guys now I know there are solutions on stack overflow I just did not understand them, here is my code
enter code here
import urllib.request import urlopen
def read_text():
quotes = open(r"C:\Users\LEON\Desktop\python\Swear_words.txt")
contents_of_file = quotes.read()
print(contents_of_file)
quotes.close()
check_profanity(contents_of_file)
def check_profanity(text_to_check):
connection =
urllib.urlopen("http://www.wdylike.appspot.com/q=s"+text_to_check)
output = connection.read()
read_text()
error
NameError: name 'urllib' is not defined
The import statement itself has incorrect syntax. It should/could be
from urllib.request import urlopen
Then the 3rd last line doesn't need the urllib prefix
urlopen("http://www.wdylike.appspot.com/q=s"+text_to_check)
How can I use the python six library for 2 and 3 compatibility on the foll. code sample:
import urllib.request
wp = urllib.request.urlopen("http://google.com")
pw = wp.read()
print(pw)
-- EDIT I tried this:
from six.moves.urllib.request import urlopen, urlretrieve
import six.moves.urllib.request as Request
request = Request('http://google.com')
but get this error:
TypeError: 'Module_six_moves_urllib_request' object is not callable
You've almost had it:
from six.moves.urllib.request import urlopen
wp = urlopen("http://google.com")
pw = wp.read()
print(pw)
Or if you wanted to addess urllib directly as in the first attempt, use from six.moves import urllib.
I am getting this error:
Traceback (most recent call last):
File "C:/Users/Shivam/Desktop/jsparse.py", line 13, in <module>
info = json.loads(str(data))
AttributeError: 'module' object has no attribute 'loads'
Any thoughts what wrong I am doing here?
This is my code:
import json
import urllib
url = ''
uh = urllib.urlopen(url)
data = uh.read()
info = json.loads(str(data))
The problem is that you're using Python 2.5.x, which doesn't have the json module. If possible, I recommend upgrading to Python 2.7.x, as 2.5.x is badly outdated.
If you need to stick with Python 2.5.x, you'll have to use the simplejson module (see here). This code will work for 2.5.x as well as newer Python versions:
try:
import json
except ImportError:
import simplejson as json
Or if you're only using Python 2.5, just do:
import simplejson as json
i have this code which searches for a word in google using google API, but for once it works fine but if i add many words or if i run it many times i keep getting the following error...
results = jsonResponse['responseData']['results']
TypeError: 'NoneType' object has no attribute '__getitem__'
i tried searching a lot on google but couldnt know what the issue is.. can anyone please help me knowing the issue and how to handle it... was struggling with this error
import urllib
import urllib2
from urllib import urlencode
import json as m_json
from urllib2 import urlopen
import re
import json
from nltk.corpus import stopwords
import sys
from urllib2 import urlopen
import urllib2
import simplejson
import pprint
words = ['headache','diabetes','myopia','dhaed','snow','blindness','head','ache','acne','aids','blindness','head','ache','acne','aids','blindness','head','ache','acne','aids']
for word in words:
url = ('https://ajax.googleapis.com/ajax/services/search/web'
'?v=1.0&q='+word+'&userip=192.168.1.105')
request = urllib2.Request(url)
response = urllib2.urlopen(request)
jsonResponse=json.loads(response.read())
#print "the response now is: ",jsonResponse
#pprint.pprint(jsonResponse)
results = jsonResponse['responseData']['results']
for result in results:
print "\nthe result is: ",result
url =result['url']
print "\nthe url is: ",url
try:
page=urllib2.urlopen(url).read()
except urllib2.HTTPError,err:
if err.code == 403:
print "bad"
continue
else:
print "good"
break
except urllib2.HTTPError:
print "server error"
except:
print "dont know the error"
thanks is advance..
Chances are that when there are no results, jsonResponse['responseData'] is None so it has no property named results in the results or responseData itself is None (== JSON null). (The dictionary lookup fails, either for jsonResponse or jsonResponse['responseData'] being null/None.
Dump the output when that error happens to see which is None and then add a check for it before the line results = jsonResponse['responseData']['results'].
Aneroid
is correct about the response data.
One possible solution to handle this:
responseData = jsonResponse['responseData']
if responseData is not None:
results = responseData['results']
for results in results:
# your code
else:
print "No Response"