Solution to the error reported by urlopen in python3urllib

Source: Internet
Author: User
This article mainly introduces the relevant information about how to solve the urlopen error in python3urllib. it is very detailed in this article. I believe it has some reference value for everyone. let's take a look at it. This article describes how to solve the urlopen error in # wiki/1514.html "target =" _ blank "> python3 urllib. This article describes in detail, I believe it has some reference value for everyone. let's take a look at it.

Preface

I recently updated the Python version and was preparing to write a crawler. unexpectedly, I found that the urllib library does not contain urlopen. Therefore, I would like to summarize all kinds of google and provide a solution.

Problem

AttributeError: 'module' object has no attribute 'urlopen'

Solution

Let's take a look at the explanation in the official document:

a new urllib package was created. It consists of code from urllib, urllib2, urlparse, and robotparser. The old modules have all been removed. The new package has five submodules: urllib.parse, urllib.request, urllib.response, urllib.error, and urllib.robotparser. The urllib.request.urlopen() function uses the url opener from urllib2. (Note that the unittests have not been renamed for the beta, but they will be renamed in the future.)

That is to say, the official version 3.0 has integrated urllib2, urlparse, and other five modules into urllib, that is, integration.

Correct usage

import urllib.request url="http://www.baidu.com" get=urllib.request.urlopen(url).read() print(get)

Result:

In fact, we can change the UTF-8 encoding to make the source code read more correct, but this is not mentioned anymore.

The above is a detailed explanation of the solution for urlopen errors in python3 urllib. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.