AttributeError: 'module' object has no attribute 'urlopen'的错误及原因


1.出现的问题

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


2.为什么出现这种问题

    (1)首先是看一下urllib包里有什么:

AttributeError: 'module' object has no attribute 'urlopen'的错误及原因_第1张图片

    突然发现并没有urllib.urlopen

    (2)再看看urllib.request里有什么

AttributeError: 'module' object has no attribute 'urlopen'的错误及原因_第2张图片

    你会发现urlopen竟然在这里(ΩДΩ)!是真滴调皮ヾ(o・ω・)ノ!

3.究其原因

    python3中官方对以前python2中的urllib2,urlparse等的一些包整合到urllib包之中了。

    官文是这样写的:

    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.) 

    所以要使用urlopen的话,你就要from urllib.request import urlopen咯~



    


你可能感兴趣的:(python3)