Python Import Urllib2 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the pip install urllib workings and policies of this site About Us Learn more about Stack import urllib.request importerror: no module named request Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions module 'urllib' has no attribute 'request' Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join importerror no module named 'urllib2' sublime them; it only takes a minute: Sign up Python3 error: “Import error: No module name urllib2” up vote 117 down vote favorite 31 Here's my code: import urllib2.request response = urllib2.urlopen("http://www.google.com") html = response.read() print(html) Any help? python python-3.x urllib2 share|improve this question edited Sep 11 at 12:43 davidism 34.3k104975 asked May 8 '10 at 1:58 delete 3
Importerror: Cannot Import Name 'urlopen'
I see you edited your answer again, so I edited my answer again to respond: your current problem is that you're saying urllib.urlopen("http://www.google.com/") instead of just urlopen("http://www.google.com/") –Eli Courtwright May 8 '10 at 15:50 add a comment| 6 Answers 6 active oldest votes up vote 148 down vote accepted As stated in the urllib2 documentation at http://docs.python.org/library/urllib2.html: The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to 3 So you should instead be saying from urllib.request import urlopen html = urlopen("http://www.google.com/") 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/"). share|improve this answer edited Dec 17 '13 at 21:44 brandonscript 19.8k1264107 answered May 8 '10 at 2:00 Eli Courtwright 84.4k44173231 1 Still getting an error, please see edit. Edit: Still getting an error when using from urllib.request –delete May 8 '10 at 2:01 2 @Sergio: It's urllib.request and not urllib2.request. The urllib and urllib2 modules from P
revision of this HOWTO, available at urllib2 - Le Manuel manquant. Introduction¶ Related Articles You may also find useful the
Urllib.request.urlopen Example
following article on fetching web resources with Python: Basic Authentication urllib.urlencode python 3 A tutorial on Basic Authentication, with examples in Python. urllib.request is a Python module for fetching URLs module 'urllib' has no attribute 'urlopen' (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different http://stackoverflow.com/questions/2792650/python3-error-import-error-no-module-name-urllib2 protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on. These are provided by objects called handlers and openers. urllib.request supports fetching URLs for many "URL schemes" (identified by the string before the ":" in URL - for example "ftp" is the URL https://docs.python.org/3/howto/urllib2.html scheme of "ftp://python.org/") using their associated network protocols (e.g. FTP, HTTP). This tutorial focuses on the most common case, HTTP. For straightforward situations urlopen is very easy to use. But as soon as you encounter errors or non-trivial cases when opening HTTP URLs, you will need some understanding of the HyperText Transfer Protocol. The most comprehensive and authoritative reference to HTTP is RFC 2616. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using urllib, with enough detail about HTTP to help you through. It is not intended to replace the urllib.request docs, but is supplementary to them. Fetching URLs¶ The simplest way to use urllib.request is as follows: import urllib.request with urllib.request.urlopen('http://python.org/') as response: html = response.read() If you wish to retrieve a resource via URL and store it in a temporary location, you can do so via the urlretrieve() function: import urllib.request local_filename, headers = urllib.request.urlretrieve('http://python.org/') html = open(
opening URLs¶ Source code: Lib/urllib/request.py The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world -- basic https://docs.python.org/3/library/urllib.request.html and digest authentication, redirections, cookies and more. See also The Requests package is recommended for a higher-level HTTP client interface. The urllib.request module defines the following functions: https://pythonhosted.org/six/ urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)¶ Open the URL url, which can be either a string or a Request object. data must be a bytes no module object specifying additional data to be sent to the server, or None if no such data is needed. data may also be an iterable object and in that case Content-Length value must be specified in the headers. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of no module named a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns an ASCII text string in this format. It should be encoded to bytes before being used as the data parameter. urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests. The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections. If context is specified, it must be a ssl.SSLContext instance describing the various SSL options. See HTTPSConnection for more details. The optional cafile and capath parameters specify a set of trusted CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capath should point to a directory of hashed certificate files. More information can be
work on both Python 2 and 3 without modification. six consists of only one Python file, so it is painless to copy into a project. Six can be downloaded on PyPi. Its bug tracker and code hosting is on BitBucket. The name, "six", comes from the fact that 2*3 equals 6. Why not addition? Multiplication is more powerful, and, anyway, "five" has already been snatched away by the (admittedly now moribund) Zope Five project. Indices and tables¶ Index Search Page Package contents¶ six.PY2¶ A boolean indicating if the code is running on Python 2. six.PY3¶ A boolean indicating if the code is running on Python 3. Constants¶ Six provides constants that may differ between Python versions. Ones ending _types are mostly useful as the second argument to isinstance or issubclass. six.class_types¶ Possible class types. In Python 2, this encompasses old-style and new-style classes. In Python 3, this is just new-styles. six.integer_types¶ Possible integer types. In Python 2, this is long and int, and in Python 3, just int. six.string_types¶ Possible types for text data. This is basestring() in Python 2 and str in Python 3. six.text_type¶ Type for representing (Unicode) textual data. This is unicode() in Python 2 and str in Python 3. six.binary_type¶ Type for representing binary data. This is str in Python 2 and bytes in Python 3. six.MAXSIZE¶ The maximum size of a container like list or dict. This is equivalent to sys.maxsize in Python 2.6 and later (including 3.x). Note, this is temptingly similar to, but not the same as sys.maxint in Python 2. There is no direct equivalent to sys.maxint in Python 3 because its integer type has no limits aside from memory. Here's example usage of the module: import six def dispatch_types(value): if isinstance(value, six.integer_types): handle_integer(value) elif isinstance(value, six.class_types): handle_class(value) elif isinstance(value, six.string_types): handle_string(value) Objec