As stated in the urllib2 documentation:
The
urllib2module has been split across several modules in Python 3 namedurllib.requestandurllib.error. The2to3tool will automatically adapt imports when converting your sources to Python 3.
So you should instead be saying
from urllib.request import urlopen
html = urlopen("http://www.google.com/").read()
print(html)
Your current, now-edited code sample is incorrect because you are saying urllib.urlopen("http://www.google.com/") instead of just urlopen("http://www.google.com/").
As stated in the urllib2 documentation:
The
urllib2module has been split across several modules in Python 3 namedurllib.requestandurllib.error. The2to3tool will automatically adapt imports when converting your sources to Python 3.
So you should instead be saying
from urllib.request import urlopen
html = urlopen("http://www.google.com/").read()
print(html)
Your current, now-edited code sample is incorrect because you are saying urllib.urlopen("http://www.google.com/") instead of just urlopen("http://www.google.com/").
For a script working with Python 2 (tested versions 2.7.3 and 2.6.8) and Python 3 (3.2.3 and 3.3.2+) try:
#! /usr/bin/env python
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
html = urlopen("http://www.google.com/")
print(html.read())
I don't think I can import urllib2 and I have no idea why.
How to use urllib2 in python3.6?
Videos
WARNING: Security researches have found several poisoned packages on PyPI, including a package named
urllib, which will 'phone home' when installed. If you usedpip install urllibsome time after June 2017, remove that package as soon as possible.
You can't, and you don't need to.
urllib2 is the name of the library included in Python 2. You can use the urllib.request library included with Python 3, instead. The urllib.request library works the same way urllib2 works in Python 2. Because it is already included you don't need to install it.
If you are following a tutorial that tells you to use urllib2 then you'll find you'll run into more issues. Your tutorial was written for Python 2, not Python 3. Find a different tutorial, or install Python 2.7 and continue your tutorial on that version. You'll find urllib2 comes with that version.
Alternatively, install the requests library for a higher-level and easier to use API. It'll work on both Python 2 and 3.
In Python 3,
urllib2was replaced by two in-built modules namedurllib.requestandurllib.error
Adapted from source
So replace this:
import urllib2
With this:
import urllib.request as urllib2
I need to download pdfs from a website. I know how, but the only way that I can think of is by using urllib2. That wouldn't be a problem, except urllib2 doesn't seem to be able to be imported. I'm using python3.4.2
This is what happens when I run a simple code to test it.
import urllib2 print(urllib2.__file__)
and this is the error I get:
Traceback (most recent call last):
File "C:/Users/ttroxell/PycharmProjects/untitled2/for_testing_functions.py", line 10, in <module>
import urllib2
File "C:\Python34\lib\urllib2.py", line 220
raise AttributeError, attr
^
SyntaxError: invalid syntaxI've tried adding the urllib2 file to any directory that has anything to do with python in a weak attempt to solve it. I honestly can't think of what else to do.
So I need to run the following code, on pycharm, but there is no availability to import the Request module (not even urllib2) and trying to even slightly adapt it results in various errors (pycharm doesn't have urllib2 for python2 either)
from urllib2 import Request, urlopen
values = """
{
"exchange_code": "GDAX",
"exchange_market": "BTC/USD",
"type": "history"
}
"""
headers = {
'Content-Type': 'application/json',
'X-API-KEY': '',
'X-API-SECRET': ''
}
request = Request('https://api.coinigy.com/api/v1/data', data=values, headers=headers)
response_body = urlopen(request).read()
print(response_body)Is there any way to get this code to run on pycharm as is, or an equivalent method that would make this work?