解决python3 urllib中urlopen报错的问题

yipeiwu_com6年前Python基础

前言

最近更新了Python版本,准备写个爬虫,意外的发现urllib库中属性不存在urlopen,于是各种google,然后总结一下给出解决方案

问题的出现

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

问题的解决途径

我们先来看下官方文档的解释:

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

也就是说官方3.0版本已经把urllib2,urlparse等五个模块都并入了urllib中,也就是整合了。

正确的使用方法

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

结果示意图:

 

其实也是可以换个utf-8的编码让读取出来的源码更正确的,但这已经是番外的不再提了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

相关文章

Python WSGI的深入理解

前言 本文主要介绍的是Python WSGI相关内容,主要来自以下网址: What is WSGI? WSGI Tutorial An Introduction t...

python中datetime模块中strftime/strptime函数的使用

python中datetime模块中strftime/strptime函数的使用

Python 的datetime模块 其实就是date和time 模块的结合,常见的属性方法都比较常用 比如: datetime.day,datetime.month,datet...

Django视图之ORM数据库查询操作API的实例

查询表记录 查询相关API 操作:models.表名.objects.方法() <BR>all(): 查询所有结果 filter(**kwargs): 它...

python+selenium实现简历自动刷新的示例代码

python+selenium实现简历自动刷新的示例代码

本文用到的文件的下载地址 百度网盘链接: https://pan.baidu.com/s/1tmpdEfAZKff5TOMAitUXqQ 提取码: e6at 1 安装Python 和 s...

如何使用python把ppt转换成pdf

ppt要想完美的转pdf,图片,还是需要在windows下面来操作。 1,安装python3.5.1 下载地址 Windows x86-64 executable installer,w...