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!