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.
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):
...
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")
CODE:
import networkx as net
from urllib.request import urlopen
def read_lj_friends(g, name):
# fetch the friend-list from LiveJournal
response=urllib.urlopen('http://www.livejournal.com/misc/fdata.bml?user='+name)
ERROR:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined
You've imported urlopen directly, so you should refer to it like that rather than via urllib:
response = urlopen('...')
You can also try in Python 3:
from six.moves import urllib
temp_file, _ = urllib.request.urlretrieve(url)
Just put import urllib at the top of your code
Try pls:
from urllib.request import urlopen
html = urlopen("http://www.google.com/")
print(html.read) # Content
For your case:
import networkx as net
from urllib.request import urlopen
def read_lj_friends(g, name):
# fetch the friend-list from LiveJournal
response=urlopen('http://www.livejournal.com/misc/fdata.bml?user='+name)
This question already has answers here:
Python 3.2 Unable to import urllib2 (ImportError: No module named urllib2) [duplicate]
(3 answers)
Closed 6 years ago.
I'm developing a Twitch chat bot in Python. However, I'm having some trouble with a feature that has been requested a lot. I need to pull the "gameserverid" and "gameextrainfo" data from a JSON file. example file
import urllib2
import json
req = urllib2.Request("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=605C90955CFDE6B1CB7D2EFF5FE824A0&steamids=76561198022404556")
opener = urllib2.build_opener()
f = opener.open(req)
json = json.loads(f.read())
currentlyPlaying = json[gameextrainfo]
gameServer = json[gameserverid]
This is the code I've got at the moment. I want to get it so that other commands can print the variables "currentlyPlaying" and "gameServer" to the IRC chat. However, when I do this, I get this in the console :
Traceback (most recent call last):
File "N:/_DEVELOPMENT/Atlassian Cloud/TwitchChatBot/Testing/grabplayerinfofromsteam.py", line 1, in <module>
import urllib2
ImportError: No module named 'urllib2'
Any ideas? I'm in a Windows environment, running on the latest version of Python (Python 3.5.1)
try:
import urllib.request as urllib2
except ImportError:
import urllib2
but dont use urllib2, use requests!
pip install requests
http://docs.python-requests.org/en/master/
I'm trying to add the sssl.SSlContext to a urlopen method but keep getting the error:
TypeError: urlopen() got an unexpected keyword argument 'context'
I'm using python 3 and urllib. This has a context parameter defined - https://docs.python.org/2/library/urllib.html. So I don't understand why it is throwing the error. But either way this is the code:
try:
# For Python 3.0 and later
from urllib.request import urlopen, Request
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen, Request
request = Request(url, content, headers)
request.get_method = lambda: method
if sys.version_info[0] == 2 and sys.version_info[1] < 8:
result = urlopen(request)
else:
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
result = urlopen(request, context=gcontext)
Can someone explain what I am doing wrong?
According to urllib.request.urlopen documentation:
Changed in version 3.4.3: context was added.
the parameter context will be added in Python 3.4.3. You need to fall back for lower version.
In Python 2.x, it's added in Python 2.7.9. (urllib.urlopen, urllib2.urlopen)
You're looking at the wrong docs. https://docs.python.org/3.0/library/urllib.request.html are the ones you want. You were using Python 2.X documentation.