I was making python http banner grabber using urllib but it gives me an error.
I was not able it import urllib2 (module not found error) so tried with with urllib instead.
Here's the code , what's wrong with it?
#!/usr/bin/env python
import urllib
url=urllib.urlopen('www.bing.com')
print (url.info())
Error:
AttributeError: 'module' object has no attribute 'urlopen'
You are using Python 3; use urllib.request instead.
import urllib.request
url = urllib.request.urlopen('http://www.bing.com')
print(url.info())
This is documented at the top of the urllib2 page:
Note: 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.
Related
I'm trying to use urllib (for backward compatibility), but I always get an error when running the script.
I tried installing urllib but got this error
ERROR: Could not find a version that satisfies the requirement urllib (from versions: none)
ERROR: No matching distribution found for urllib
Python version - 2.7.16
this is the import part in the script which fails:
from urllib import request, parse
from urllib import error as urllib_error
import error:
ImportError: cannot import name request
Please advise.
From the 2.7 documentation :
The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
So if you are trying to import urllib.request, parse and error separately - that constructs are available in Python 3.x version.
Please go through the documentation for 2.7 at : https://docs.python.org/2.7/library/urllib.html
Or upgrade to latest verion of Python 3.x to import the way you have described in your post.
You should import like this:
Download latest version of python from here
import urllib.request,urllib.parse, urllib.error
There is no urllib.request module in Python 2, that module only exists in Python 3, You can use urllib2 and Request:
from urllib2 import Request
The issue was that my environment was configured to use Python2 and not Python3, once I resolved this, the import worked as expected.
I coded this:
from datetime import datetime, timedelta
import json
import time
import urllib2
...
req=urllib2.Request(api_url,binary_data,header)
f=urllib2.urlopen(req)
My python version is the 3.6.5 so i´m supposed to have the urllib2 installed already but every time i get this error:
import urllib2
ModuleNotFoundError: no module named 'URLLIB2'
I changed the name to urllib3 as it appears in my anaconda folder but it crashes anyway....
what do i do?
Urllib2 is meant for Python 2, it is no longer used in Python 3. The standard module is now called urllib (you can find the documentation for it here: https://docs.python.org/3/library/urllib.html).
Try this instead:
import urllib.request
req=urllib.request.Request(api_url,binary_data,header)
f=urllib.request.urlopen(req)
urllib.request.Request(): https://docs.python.org/3/library/urllib.request.html#urllib.request.Request
urllib.request.urlopen():https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen
When I run the following code in Python 3.3:
import urllib
tempfile = urllib.request.urlopen("http://yahoo.com")
I get the following error:
I did this too to verify:
What am I doing wrong?
The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
Import urllib.request instead of urllib.
import urllib.request
Interestingly, I noticed some IDE-depending behavior.
Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do
import urllib.request
while in Spyder,
import urllib
does fine
If this is on PyCharm, as was mine, make sure your file name isn't urllib.py.
In visual code , u have to write import urllib.request instead of just import urllib.
Also, whenever errors such as module x has no attribute y occurs, it's because you have named the current file same as the package you are trying to import.
So, the way import in python works is that it first searches the current dir, and if it finds the module/package 'x' u were looking for , it assumes that it has found the target file, and searches for 'y'. And since u haven't defined 'y', the aforementioned error occurs.
I installed lxml on a mac and trying to use it in my code and I get errors importing tostring and tounicode. Python cannot see it at all. Any ideas what I am doing wrong?
Here is the code that is causing problems -
from lxml.etree import tostring
from lxml.etree import tounicode
I get an unresolved import error
Also my IDE (eclipse) is able to see the init.py file for lxml.etree module. Here is what it sees ---
# this is a package
def get_include():
"""
Returns a list of header include paths (for lxml itself, libxml2
and libxslt) needed to compile C code against lxml if it was built
with statically linked libraries.
"""
import os
lxml_path = __path__[0]
include_path = os.path.join(lxml_path, 'includes')
includes = [include_path, lxml_path]
for name in os.listdir(include_path):
path = os.path.join(include_path, name)
if os.path.isdir(path):
includes.append(path)
return includes
Thanks for any help.
EDIT:
The only log I see is
Unresolved import: tostring
Unresolved import: tounicode
And when I add the following line before the import of tostring it works no errors --
import etree from lxml
Also to give you some more background on what I am trying to do. I got the readability code from here (https://github.com/buriy/python-readability) and trying to use it in my project.
EDIT2: I fixed the problem but still do not understand why. I wanted to use the code from the readability project directly without installing the package using easy_install. The idea was step into the code so that I understand what its doing. But when I copy the code into my project I get the above mentioned error in the readability code. If I install the package using easy_install then all I can simply import the class exported by readability and use it.
So can some one tell me what the difference is between using the code directly and the package installed? What is a .egg file? How to create one?
In lxml's code, it dynamically load modules. That makes IDE fails to analyze reference as IDE just analyzes raw code.
I've found this solution extremely useful when trying to resolve the resource and import etree in my Intellij IDEA Python project:
Have a look at lxml tutorial which suggests this solution:
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
I have looked everywhere for this and just cannot find the answer. I have checked my python version and it is version 3.2 . When I try to import cookielib I receive:
ImportError: No module named cookielib
I have seen that in Python 3.0 it was renamed to
http.cookiejar and that it would auto import cookielib.
I thought that maybe there was some wild error in my python configuration so I thought I should try and import http.cookiejar like this import http.cookiejar. That did not work all and I get and error:
EOFError: EOF read where not expected.
This is not the error I had expected becuase import http.cookies imports just fine.
Does anybody have a solution to this problem? What am I overlooking?
Full Error:
Traceback (most recent call last):
File "C:\Users\Spencer\Downloads\selenium-2.20.0.tar\selenium-2.20.0\selenium-2.20.0\test", line 1, in <module>
import urllib.request, urllib.parse, http.cookiejar
EOFError: EOF read where not expected
The automatic renaming business only applies if you use 2to3. Therefore, you have to import http.cookiejar.
The error EOFError: EOF read where not expected is only ever thrown by Python marshalling. Most likely, this is caused by a race condition fixed in Python 3.3, where multiple processes tried to write concurrently to the pyc file. Deleting all .pyc files may be a workaround.
try:
import cookielib
except:
import http.cookiejar
cookielib = http.cookiejar
The cookielib module has been renamed to http.cookiejar in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.
My initial guess is that you have a corrupted library file. Inside your Python installation, look at lib/python3.2/http/cookiejar.py and scroll down to the end. Mine (Python 3.2.2) ends in the save() method definition with
finally:
f.close()
If you see anything else, your installation is probably broken and I'd recommend reinstalling it.